views:

576

answers:

3

This is something I found myself spending hours to figure out and therefor wants to share with you.

So the question was, How do i determine the day of the year for a specific date?

e.g. January 15 is the 15'th day and December 31 is the 365'th day when it's not leap year.

A: 

With the NSDate class. Use message timeIntervalSinceDate. It will return you a NSTimeInterval value (actually it's a double) that represents the seconds elapsed since the date you want. After that, it's easy to convert seconds-to-days.

If you truncate seconds/86400 will give you days.

Let's say you want days since January 1st 2010.

// current date/time
NSDate *now = [[NSData alloc] init]; 

// seconds elapsed since January 1st 2010 00:00:00 (GMT -4) until now
NSInteval interval = [now timeIntervalSinceDate: [NSDate dateWithString:@"2010-01-01 00:00:00 -0400"]];

// days since January 1st 2010 00:00:00 (GMT -4) until now
int days = (int)interval/86400;
Pablo Santa Cruz
Don't forget to add one, as the value that you get is zero based.
Guffa
The problem is that you'll have to test the year for each date before you can calculate. You don't want January 1, 2011 to be the 366'th day of the year
Godisemo
As Godisemo says, you can't rely on large constants for figuring out days. What about leap seconds as well? Or weird years in the distant past missing whole weeks of days?
Kendall Helmstetter Gelner
+2  A: 

So the solution I came up with is pretty neat and simple and not in any way complicated.

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"D"];
NSUInteger dayOfYear = [[formatter stringFromDate:[NSDate date]] intValue];
[formatter release];
return dayOfYear;

The trick here which I spent so long time to figure out was to use the NSDateFormatter. The "D" is the flag for day of year.

Hope that this was helpful to you who have the same problem as I had.

Godisemo
A neat solution, but not the neatest. ;) I think that you got a bit stuck in thinking of dates in units of years, months and days instead of as a continuous time line. Dates are often stored as elapsed seconds (or days or milliseconds or 1/10000000 seconds) from a set date, a format that makes it very easy to get the difference from the start of the year to the specific date.
Guffa
true, that's the hard thing when solving problems. You get stuck in one approach and can't think of alternatives. Thanks
Godisemo
+10  A: 

Try this:

NSCalendar *gregorian =
   [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];
NSUInteger dayOfYear =
   [gregorian ordinalityOfUnit:NSDayCalendarUnit
     inUnit:NSYearCalendarUnit forDate:[NSDate date]];
[gregorian release];
return dayOfYear;

where date is the date you want to determine the day of the year for. The documentation on the NSCalendar.ordinalityOfUnit method is here.

John Feminella
wow, this could simplify things, it's almost an answer to my other question, http://stackoverflow.com/questions/2078372/ Propably a better one :)
Godisemo