Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/iphone/39.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 如何在iphone中计算当前日期的天数_Ios_Iphone_Ios7 - Fatal编程技术网

Ios 如何在iphone中计算当前日期的天数

Ios 如何在iphone中计算当前日期的天数,ios,iphone,ios7,Ios,Iphone,Ios7,我正在开发一个iPhone应用程序 如何在iPhone中获取从当前年份第一个日期到当前日期的天数 我尝试使用以下代码: NSCalendar*gregorianCalendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar] NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:self t

我正在开发一个iPhone应用程序

如何在iPhone中获取从当前年份第一个日期到当前日期的天数

我尝试使用以下代码: NSCalendar*gregorianCalendar=[[NSCalendar alloc]initWithCalendarIdentifier:NSGregorianCalendar]

NSDateComponents *components = [gregorianCalendar components:NSDayCalendarUnit fromDate:self toDate:aDate options:0];

return [components day];
但当我给出的起始日期是:2013年1月1日至12月31日时,我得到了363天。但我们有365天


请帮我解决这个问题。谢谢。

使用此代码,您可以获得两个日期之间的天数

NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *components = [gregorian components:NSDayCalendarUnit
                                                fromDate:date

                                                  toDate:toDate
                                                 options:0];
    NSInteger days = [components day];
    ;
    return days;
NSCalendar是你的朋友:

NSDate *now = [NSDate date];
NSCalendar *cal = [NSCalendar currentCalendar];
NSUInteger days = [cal ordinalityOfUnit:NSDayCalendarUnit
                                 inUnit:NSYearCalendarUnit forDate:now];

今天的结果2013年12月11日:345。

什么???????????当你把我搞糊涂的时候,我在iPhone上从第一次到现在都能理解它。请告诉我们你已经尝试了什么,以及你的代码有什么问题@大力水手你有我的问题吗?我的初步印象是你想知道从2013年1月1日的x日到今天的y日有多少天。对吗?如果是的话,请分享你的尝试,让你的问题更有意义。@have Friend我需要获得从当前年份第一个日期到当前日期的天数。我需要从当前年份中获取当前日期全天。它工作得很好,谢谢friend@user1547937:如果我的答案有帮助,那么如果你点击复选标记接受它,那就太好了。