Getting week and day number from NSDate

Discussion in 'Mac Programming' started by xmax, Mar 12, 2009.

  1. macrumors member

    Joined:
    Jul 3, 2008
    #1
    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.
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Yes, or them together like NSDayCalendarUnit | NSWeekCalendarUnit and then the returned NSDateComponents object will contain values for both.
     
  3. thread starter macrumors member

    Joined:
    Jul 3, 2008
    #3
    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.
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    Maybe NSWeekCalendarUnit is what you want (have you looked at the docs?).
     
  5. thread starter macrumors member

    Joined:
    Jul 3, 2008
    #5
    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
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    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.
     

Share This Page