NSCalendarDate Month to Int

Discussion in 'Mac Programming' started by justmyself, Jan 2, 2009.

  1. justmyself macrumors member

    Joined:
    Jan 2, 2009
    #1
    I'm looking to get the current month in an integer. ie 0 - 11. I've spend quite bit of time on apples dev site for NSCalendarDate, but to no avail. Much frustration. Any help would be appreciated.

    Thanx
     
  2. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #2
    This is likely not the best way, but here's an example of something related.

    Code:
    NSCalender *cal = [NSCalendar autoupdatingCurrentCalendar];
    NSDateComponents *comps = [cal components: NSMonthCalendarUnit fromDate:[NSDate date]];
    NSInteger month = [comps month];
    
     
  3. justmyself thread starter macrumors member

    Joined:
    Jan 2, 2009
    #3
    Yeeps, so overwhelming. I tried your example but for some reason I got a several errors. I'm hoping to find something rather strait forward.

    ie.

    int myVariable;

    myVariable = NSCalendarDate etc... etc...


    Obviously it won't be that simple, but as someone new to Objective C and Cocoa I need something quite basic. Is it just me or does Objective C and Cocoa have a steep learning curve? I've done C++, Perl, as well as JavaScript and Unix Scripting with no problems. This however just seems so twisted.
     
  4. cazlar macrumors 6502

    Joined:
    Oct 2, 2003
    Location:
    Sydney, Australia
    #4
    Is it not as simple as:
    Code:
    NSCalendarDate *now = [NSCalendarDate calendarDate];
    int i = [now monthOfYear];
    ??


    BTW, That should give 1-12 I believe, not 0-11


    Though the above NSDateComponents code is the way to do it on iPhone, since for some reason it lacks NSCalendarDate (grrr).
     
  5. Catfish_Man macrumors 68030

    Catfish_Man

    Joined:
    Sep 13, 2001
    Location:
    Portland, OR
    #5
    I believe you're correct cazlar. It was 2AM when I posted my code and I didn't feel like checking for other ways to do it ;)
     

Share This Page