1

I need to know day of year with iOS sdk. I can do that manually, but wanted to know that is there any method to find day of year? I am not meaning that - How many days in a year. I need to know the number of day for a input date?

Hal Mueller
  • 7,019
  • 2
  • 24
  • 42
arefin
  • 365
  • 3
  • 17
  • They explain it in a WWDC video if I recall. I can find it again if you wish to watch it (You have to use *NSCalendar* i think) – Jean Apr 03 '13 at 20:55
  • @Jean Think you're referring to [Performing Calendar Calculations](https://developer.apple.com/itunes/?destination=adc.apple.com.8270634034.08270634040.8367260919?i=2132147479) from WWDC 2011, video available in the Mac Dev Center (free) – Jay Apr 04 '13 at 11:44
  • @Jay Yes, this is the one. One only needs to have a (free) developer ID to access it. Thanks for providing the link. – Jean Apr 04 '13 at 16:20

1 Answers1

4

Got answer :) please see the below example

NSCalendar *currentCalendar = [NSCalendar currentCalendar];
    NSDate *today = [NSDate date];
    NSInteger dc = [currentCalendar  ordinalityOfUnit:NSDayCalendarUnit
                                               inUnit:NSYearCalendarUnit
                                              forDate:today];
    NSLog(@"%d",dc);
arefin
  • 365
  • 3
  • 17
  • Then you can mark your answer as accepted. And I'll vote it up for you. – Jean Apr 03 '13 at 20:58
  • You can also do it with NSDateFormatter, with the "D" formatting code. – Hot Licks Apr 03 '13 at 21:01
  • @HotLicks: Washing machine is made to wash clothes, not to mix sugar with water. i.e., NSCalender and NSDateComponents are meant to do this kind of thing, **we shouldn't use NSDateFormatter** for these. This is what I learnt from SO – Anoop Vaidya Apr 04 '13 at 02:28
  • @AnoopVaidya - But my wife uses a washing machine to felt knitted stuff. Sometimes the wrong tool is the right tool. – Hot Licks Apr 04 '13 at 11:34
  • @AnoopVaidya - By the way, residential dishwashers are often used in electronics manufacturing for cleaning parts. – Hot Licks Apr 04 '13 at 15:36
  • @HotLicks: You must be knowing about Punjab and Lassi (curd mixed with butter, cardamom, sugar). Few years back suddenly lot of demand of washing machine came from there. After investigation they came to know all juice-centre, restaurant purchased wash-mach for mixing curd+water+sugar as it has more capacity than a small Kitchen Mixer-grinder. :) – Anoop Vaidya Apr 04 '13 at 16:29