View Full Version : Getting week and day number from NSDate

Mar 12, 2009, 05:48 AM
I want to get week number and day number from given date.
Say Given date is "2009-03-12 16:11:09 +0530". On providing this date, it should give me result second thursday i.e 2 for second and 5 for thursday.
I am trying to get it using NSDateComponents and NSDayCalendarUnit.

Is it possible to have a day and week number for particular date ?


Mar 12, 2009, 05:50 AM
Yes, or them together like NSDayCalendarUnit | NSWeekCalendarUnit and then the returned NSDateComponents object will contain values for both.

Mar 12, 2009, 06:06 AM
I got 5 i.e day number. but i am not getting week number.
I have done this much on getting day number
NSCalendar *gregorian = [NSCalendar currentCalendar];
NSDateComponents *weekDayComponents = [gregorian components:NSWeekdayCalendarUnit fromDate:aDate];
NSInteger mDay = [weekDayComponents weekday];
NSLog(@"day number = %d" , mDay);

I am not getting week number.


Mar 12, 2009, 06:38 AM
Maybe NSWeekCalendarUnit is what you want (have you looked at the docs?).

Mar 12, 2009, 07:53 AM
Yes, but it gives me the week number for year that is 11.
But we need week number for that month only i.e 2.

so, is there any way ot get it?


Mar 12, 2009, 08:31 AM
Ok this is basic logic. If those two values don't give you what you want, look at the documentation and/or header file and see if there is another one that does.