26

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

The question was: How do I determine the day of the year for a specific date?

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

John Feminella
  • 303,634
  • 46
  • 339
  • 357
Godisemo
  • 1,813
  • 2
  • 18
  • 26

6 Answers6

76

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.

David
  • 3,285
  • 1
  • 37
  • 54
John Feminella
  • 303,634
  • 46
  • 339
  • 357
  • wow, this could simplify things, it's almost an answer to my other question, http://stackoverflow.com/questions/2078372/ Propably a better one :) – Godisemo Jan 17 '10 at 12:53
9

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
  • 1,813
  • 2
  • 18
  • 26
  • 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 Jan 17 '10 at 12:36
  • true, that's the hard thing when solving problems. You get stuck in one approach and can't think of alternatives. Thanks – Godisemo Jan 17 '10 at 12:55
  • How do I use this method to get islamic day of the year? – Akshatha S R Mar 14 '17 at 06:03
  • this is an oldie but .....I have an app where I'm still using it.. the issue I ran is for a leap year e.g. Jan 01 2020 the day of year should be 0 but it pops as 365. – becker Mar 16 '21 at 23:16
5

Using ARC and replacing deprecated symbols, John Feminella's answer would look like this:

NSCalendar *greg    = [NSCalendar calendarWithIdentifier:NSCalendarIdentifierGregorian];
NSUInteger dayOfYear= [greg ordinalityOfUnit:NSCalendarUnitDay inUnit:NSCalendarUnitYear forDate:[NSDate date]];
Mojo66
  • 1,109
  • 12
  • 21
0

Swift 4 variation:

let date = Date() // now
let calendar = Calendar(identifier: .gregorian)
let dayOfYear = calendar.ordinality(of: .day, in: .year, for: Date()) 
wm.p1us
  • 2,019
  • 2
  • 27
  • 38
0

for Apple Swift version 5.4.2

    let formatter = DateFormatter()
    formatter.dateFormat = "d"
    let day = formatter.string(from: Date())
    
    if let dd = Int(day){
        print("day: \(dd)")
    }
black_pearl
  • 2,549
  • 1
  • 23
  • 36
-2

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
  • 176,835
  • 32
  • 241
  • 292
  • Don't forget to add one, as the value that you get is zero based. – Guffa Jan 17 '10 at 12:31
  • 1
    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 Jan 17 '10 at 13:09
  • 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 Jan 17 '10 at 19:02