可能重复:
How do you calculate the day of the year for a specific date in Objective C当前日年
我正在寻找一种方式来发现其中有过去在当年天数。例如,如果今天是2013年3月22日,那么该数字应该是81,因为81天已经过去了2013年。不管它是否是闰年都不重要。
所以,如果任何人有一些链接,甚至如何做到这一点,请与我分享。 在此先感谢!
可能重复:
How do you calculate the day of the year for a specific date in Objective C当前日年
我正在寻找一种方式来发现其中有过去在当年天数。例如,如果今天是2013年3月22日,那么该数字应该是81,因为81天已经过去了2013年。不管它是否是闰年都不重要。
所以,如果任何人有一些链接,甚至如何做到这一点,请与我分享。 在此先感谢!
您可以通过此
NSDate *date1 = [NSDate dateWithString:@"2010-01-01 00:00:00 +0000"];
NSDate *date2 = Your END Date/Today's date may be;
NSTimeInterval secondsBetween = [date2 timeIntervalSinceDate:date1];
int numberOfDays = secondsBetween/86400;
NSLog(@"No of days passed since new year.", numberOfDays);
不是所有的日子都有86400秒(由于DST,闰秒等)。 –
试试这个:
NSCalendar *currentCalendar = [NSCalendar currentCalendar]; NSDate *today = [NSDate date]; NSInteger dc = [currentCalendar ordinalityOfUnit:NSDayCalendarUnit inUnit:NSYearCalendarUnit forDate:today];
http://stackoverflow.com/questions/2080927/how-do-you-calculate-the-day-of-特定日期在目标-c看起来像一个确切的重复 –
谢谢,试着用谷歌搜索它,并在这里找到它在stackoverflow,但没有设法找到这个。这正是我需要的。非常感谢您的帮助! – Lukas
你可以尝试使用unix时间戳,因为它是通用的...你可以使用它来操作任何日期 – madLokesh