可能重複:
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