Search code examples
iosnumbersnsdatedaysleap-year

iOS How to get day number of the year?


Possible Duplicate:
How do you calculate the day of the year for a specific date in Objective C

I need to get the day number of the year and don't know which would be the best aproach.

Example:

21-jan-2012 -> 21

2-feb-2012 -> 33

2-mar-2012 -> 62

2-mar-2011 -> 61

Any ideas?

Thanks


Solution

  • Look at NSTimeInterval. You can get the seconds elapsed between two dates:

    NSTimeInterval secondsElapsed = [secondDate timeIntervalSinceDate:firstDate];
    

    So your firstDate can be the beginning of the year. Then you can just divide by 3600*24 (seconds per day) to get the days.