How to convert string to NSDate

Discussion in 'iOS Programming' started by mikezang, Aug 30, 2010.

  1. mikezang macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #1
    I got a date string in format "Sun, 20 Aug 2010 12:31:09 GMT", I tried to convert it as below, but it is wrong, how can I convert it ?

    Code:
    NSString *lastModifiedString = [metaData objectForKey:@"Last-Modified"];
    NSLog(@"%@", lastModifiedString);
    NSDateFormatter *dateFormat = [[[NSDateFormatter alloc] init] autorelease];
    [dateFormat setDateFormat:@"%a, %d %m %Y %H %Z"];
    NSDate *lastModifiedDate = [dateFormat dateFromString:lastModifiedString]; 
    
     
  2. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #2
    Does your format string look like the example given under the "Format Strings" heading of the "Date Formatters" reference guide?
    http://developer.apple.com/iphone/l...Formatting/Articles/dfDateFormatting10_4.html
    (Red hilite added for emphasis.)
    Code:
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:[COLOR="Red"]@"yyyy-MM-dd 'at' HH:mm"[/COLOR]];
     
    NSDate *formatterDate = [inputFormatter dateFromString:@"1999-07-11 at 10:30"];
     
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:[COLOR="Red"]@"HH:mm 'on' EEEE MMMM d"[/COLOR]];
     
    NSString *newDateString = [outputFormatter stringFromDate:formatterDate];
     
    NSLog(@"newDateString %@", newDateString);
    // For US English, the output is:
    // newDateString 10:30 on Sunday July 11
    
    You may have to read the complete reference doc regarding format strings.
     
  3. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
  4. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #4
    Thought I got answer, I am still not sure about difference of two format, can you explain for me?
    Both as below from
    http://developer.apple.com/mac/libr...id/20000188--dateWithCalendarFormat_timeZone_

    Code:
    formatString
        The format for the returned string (see Converting Dates to Strings 
    for a discussion of how to create the format string). 
    Pass nil to use the default format string, 
    “%Y-%m-%d %H:%M:%S %z” 
    (this conforms to the international format 
    YYYY-MM-DD HH:MM:SS ±HHMM.)
    
    Code:
    Return Value
    
    A string representation of the receiver in the international format
    YYYY-MM-DD HH:MM:SS ±HHMM, where ±HHMM 
    represents the time zone offset in hours and minutes from GMT (for example, 
    “2001-03-24 10:45:32 +0600”).
     
  5. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #5
    Why would you be using the Mac class NSCalendarDate on the iPhone?

    The method you pointed to should not be used on Mac OS X, and doesn't even exist in the iOS version of NSDate.
    http://developer.apple.com/iphone/l...Classes/NSDate_Class/Reference/Reference.html

    There is no NSCalendarDate class on iOS at all.

    When you have an NSDate, the best way to convert it to an NSString in a particular format is to use an NSDateFormatter. This is true for Mac OS and iOS.

    The example I pointed to (and quoted, with red hilites) before shows exactly this. There is an inputFormatter, which parses a date string in one format. There is an outputFormatter, which converts the resulting NSDate into a different string format.


    The reason the Mac method NSDate dateWithCalendarFormat:timeZone: mentions the format string "%Y-%m-%d %H:%M:%S %z" is because that kind of format string is an older form used in Mac OS versions 10.0 thru 10.3. That was changed as of Mac OS 10.4. The newer, more powerful formatting strings (and the only kind available to iOS) follows the international standard.
    http://developer.apple.com/mac/libr...00103.html#//apple_ref/doc/uid/TP40007972-SW9
     
  6. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #6
    Well, now I understand the reason behind two formats.

    You know, there are a lot of documents in dev.apple, it is too hard to a newbie.
     
  7. chown33 macrumors 604

    Joined:
    Aug 9, 2009
    #7
    You don't have to read them all at once. You don't even have to read them all, as long as you know how to find them. I've gone a long time without knowing all the details of CFRunLoop, but I know where to look when I do need to know the details.

    You can immediately tell whether something is a Mac document or an iOS document by looking at the URL:
    //developer.apple.com/iphone/library/..etc
    //developer.apple.com/mac/library/..etc

    Also, most books will condense and summarize the most commonly used classes and their methods. Converting dates isn't rare or esoteric, so most books cover things like that.
     
  8. Mateusz macrumors newbie

    Joined:
    Nov 17, 2010
    #8
    I was trying to do the same and after some struggling with documentation in XCode I came across this link.
    http://unicode.org/reports/tr35/#Date_Format_Patterns

    And here's working solution. Hope it helps.

    Code:
    NSDateFormatter *df = [[NSDateFormatter alloc] init];
    	
    [df setDateFormat:@"E, d M y H:m:s Z"];
    NSDate *date = [df dateFromString:@"Sun, 06 Nov 1994 08:49:37 GMT"];
    	
    NSLog(@"%@", date);
    [df release];
     
  9. mikezang thread starter macrumors 6502a

    Joined:
    May 22, 2010
    Location:
    Tokyo, Japan
    #9
    Thanks, I refered the same link as you as below:
    From NSString to NSDate
    Code:
    NSDateFormatter *inputFormatter = [[NSDateFormatter alloc] init];
    [inputFormatter setDateFormat:@"yyyy-MM-dd 'at' HH:mm"];
    NSDate *formatterDate = [inputFormatter dateFromString:@"1999-07-11 at 10:30"];
    
    NSLog(@"Date %@", formatterDate);
    [inputFormatter release];
    
    From NSDate to NSString
    Code:
    NSDateFormatter *outputFormatter = [[NSDateFormatter alloc] init];
    [outputFormatter setDateFormat:@"HH:mm 'on' EEEE MMMM d"];
    NSString *newDateString = [outputFormatter stringFromDate:formatterDate];
    
    NSLog(@"newDateString %@", newDateString);
    [outputFormatter release];
    
     

Share This Page