2

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

Community
  • 1
  • 1
user1078065
  • 412
  • 1
  • 5
  • 19

4 Answers4

22

I'm a little late for the party but anyway, here it goes:

 NSDate *today = [NSDate date];
 NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"DDD"];
 NSInteger dayInYear = [[dateFormat stringFromDate:today] integerValue];
 NSLog(@"today is %d. day in year", dayInYear);

result for 14.jun.2012: today is 166. day in year

EDIT: i was amazed that there was no easy-to-find example on Google.

You could also use format @"DD" to get day in month or @"D" to get day in week.

EDIT2: Just a note: creating NSDateFormatter can be quite expensive operation. If your class uses this function frequently you might want to create a static formatter ivar.

Rok Jarc
  • 18,765
  • 9
  • 69
  • 124
-1

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.

davidethell
  • 11,708
  • 6
  • 43
  • 63
  • This is actually not correct, I'm new to the topic but using NSCalendar (or NSDateFormatter) would be the safer approach: http://infiniteundo.com/post/25326999628/falsehoods-programmers-believe-about-time – Chris Conover Nov 20 '13 at 20:58
  • The [docs](https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/DatesAndTimes/Articles/dtCalendars.html#//apple_ref/doc/uid/TP40003470-SW7) also cover this: "If you instead want to know what number day of the year it is you can use the ordinalityOfUnit:inUnit:forDate: method of the NSCalendar class." – Chris Conover Nov 20 '13 at 21:07
-1

Check out the below solution You have to use timeIntervalSinceDate: for this

    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init] ;
    [dateFormatter setDateFormat:@"yyyy-MM-dd"];

    NSDate *date1 = [dateFormatter dateFromString:@"2012-01-01"];
    NSDate *date2 = [NSDate date]; 
    NSLog(@"date %@ ", date2);

    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@"yyyy-MM-dd"];

    NSDateFormatter *timeFormat = [[NSDateFormatter alloc] init];
    [timeFormat setDateFormat:@"HH:mm:ss"];

    NSDate *now = [[NSDate alloc] init];

    NSString *theDate = [dateFormat stringFromDate:now];
    NSDate *dt = [dateFormat dateFromString:theDate];


    NSTimeInterval diffDays = [dt timeIntervalSinceDate:date1];
    NSLog(@"Date %f ^^^^ ", diffDays/(24*60*60));
Hiren
  • 12,720
  • 7
  • 52
  • 72
-3

At first You create a Dictionnary like :

[Jan => 30,
Feb =>  28,
 ...] 

With all days per month.

  • Secondly with your date like : 13 June 2012.
  • You'll add all value of the month before june (a while on your dictionnary).
  • And finish by add the day (here : 13).
Damien Locque
  • 1,810
  • 2
  • 20
  • 42