PDA

View Full Version : Getting week and day number from NSDate




xmax
Mar 12, 2009, 05:48 AM
Hello,
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 ?


Thanks.



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

xmax
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.




Thanks.

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

xmax
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?


Thanks

kainjow
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.