Skip navigation
This discussion is archived

How to get day of week from NSDate?

29160 Views 9 Replies Latest reply: Dec 9, 2008 5:15 PM by DarelRex RSS
aapl.crox Level 1 Level 1 (0 points)
Currently Being Moderated
Sep 8, 2008 5:08 PM
How to get day of week from NSDate?
  • GazF Calculating status...
    Currently Being Moderated
    Sep 8, 2008 8:19 PM (in response to aapl.crox)
    something like this:


    NSCalendar *gregorian = [[NSCalendar alloc] initWithCalendarIdentifier:NSGregorianCalendar];

    NSDateComponents *weekdayComponents =[gregorian components:NSWeekdayCalendarUnit fromDate:dateOfInterest];

    NSInteger weekday = [weekdayComponents weekday];
    // weekday 1 = Sunday for Gregorian calendar

    [gregorian release];
    Mac OS X (10.5.3)
  • brec Level 1 Level 1 (15 points)
    Currently Being Moderated
    Sep 8, 2008 8:22 PM (in response to aapl.crox)
    I entered "day of week" into the Xcode's Documentation search box and the first hit looks like it might be useful: the Calendrical Calculations article in Date and Time Programming Guide for Cocoa.
    MacBook Pro 17, Mac OS X (10.5.4)
  • Steve Herman1 Level 4 Level 4 (2,545 points)
    Currently Being Moderated
    Sep 8, 2008 9:41 PM (in response to aapl.crox)
    Here's a way to get the name of the weekday as an NSString if you've already got an NSDate...

    NSDate * testDate = [NSDate date];
    NSString * weekdayString = [testDate descriptionWithCalendarFormat:@"%A" timeZone:nil
    locale:[[NSUserDefaults standardUserDefaults] dictionaryRepresentation]];
    NSLog(@"Day of the week: %@", weekdayString);

    The output is something like:

    Day of the week: Monday

    You can use a lower-case "%a" as the descriptionWithCalendarFormat if you just want the three character weekday abbreviation ("Mon", "Tue", "Wed", etc).

    Steve
    20" iMac G5, Mac OS X (10.5.4)
  • GazF Level 1 Level 1 (40 points)
    Currently Being Moderated
    Sep 8, 2008 11:28 PM (in response to Steve Herman1)
    If you're developing for the iPhone, NSDate's descriptionWithCalendarFormat isn't available. (for some reason..)
    Mac OS X (10.5.3)
  • jjss2810 Calculating status...
    Currently Being Moderated
    Oct 7, 2008 9:37 AM (in response to GazF)
    For the iphone this code will work:



    - (NSString *)getDayOfTheWeek:(NSDate *)date{
    NSDateFormatter *dateFormatter = [[[NSDateFormatter alloc]
    initWithDateFormat:@"%A" allowNaturalLanguage:NO] autorelease];
    NSString *formattedDateString = [dateFormatter stringFromDate:date];
    return formattedDateString;
    }

    MacBook, Mac OS X (10.5.4), none
  • DarelRex Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 9, 2008 4:29 PM (in response to aapl.crox)
    How about this:

    NSString *myString=[myDate descriptionWithCalendarFormat:"%w" timezone:nil locale:nil] ;

    That should return the string "0" for Sunday, "1" for Monday, etc.
    Mac Mini, Mac OS X (10.5.5)
  • DarelRex Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 9, 2008 4:33 PM (in response to aapl.crox)
    Let's try that again! Formatted correctly this time:



    How about this:

    NSString *myString=\[myDate descriptionWithCalendarFormat:"%w" timezone:nil locale:nil\] ;

    That should return the string "0" for Sunday, "1" for Monday, etc.
    Mac Mini, Mac OS X (10.5.5)
  • briyu Level 1 Level 1 (100 points)
    Currently Being Moderated
    Dec 9, 2008 4:56 PM (in response to DarelRex)
    I like this approach. I've been looking for something like this to use in one of my applications, and I'm surprised I didn't think of this before.
    Mac OS X (10.5.5)
  • DarelRex Level 1 Level 1 (0 points)
    Currently Being Moderated
    Dec 9, 2008 5:15 PM (in response to aapl.crox)
    I forgot the at-sign and misspelled timeZone -- here is the corrected version:



    NSString *myString=\[myDate descriptionWithCalendarFormat:@"%w" timeZone:nil locale:nil\] ;

    Message was edited by: DarelRex
    Mac Mini, Mac OS X (10.5.5)

Actions

More Like This

  • Retrieving data ...

Bookmarked By (1)

Legend

  • This solved my question - 10 points
  • This helped me - 5 points
This site contains user submitted content, comments and opinions and is for informational purposes only. Apple disclaims any and all liability for the acts, omissions and conduct of any third parties in connection with or related to your use of the site. All postings and use of the content on this site are subject to the Apple Support Communities Terms of Use.