Date Formatter Examples – Take 3: Date from String

While working on an iPhone application recently, I needed to convert a date read from an XML stream that was in the following format: 20081122 to a nicely formatted string for display on the device: Saturday November 22, 2008.

How to get there from here is now obvious, however, when I first encountered this dilemma the solution wasn’t apparent. The reason being, there is significant depth in the Cocoa frameworks and half the battle in becoming proficient as an iPhone developer is to have an opportunity to explore the range of APIs. Albeit the solution was right under my nose the whole time, my first pass was to take a more traditional route of trying to parse the string and rebuild a more “traditional” date format which I could use to create a date object. So, skipping all that, here’s the proper solution…

If you’ve ever worked with dates in Cocoa, chances are you are familiar with the stringFromDate method of the NSDateFormatter. For example, the code below will convert the current date to a string that looks like this: Wednesday November 26, 2008

The trick is two-fold, the date format string to specify desired output, and the method stringFromDate to convert the date object to an NSString.

I’m sure you can see where I’m going with this…the solution I was looking for to convert a date (stored as a string) that was in a pre-defined format (i.e. 20081122) to a date object is as simple as using the method dateFromString. The primary difference is that the format string needs to represent the current format of the date that is to be read (versus the desired output format).

The code below converts a string that represents a date to an NSString object, with the output as follows: Saturday November 22, 2008:

More on Date Formatting:

Date Formatter Examples – Take 1: NSDateFormatter
Date Formatter Examples – Take 2: Format Strings
Date Formatter Examples – Take 4: Setting Locale

24 Comments

  1. I am having troubles with parsing am/pm signs in date. Can you please advise me on how to do that?

  2. NSDate *date = [NSDate date];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@”EEEE MMMM d, YYYY”];
    NSString *dateString = [dateFormat stringFromDate:date];
    [dateFormat release];

    i have tried this…but it always shows as Jan 08,2010…but the date i am passing here is 2010-10-08..

    why this happens..?

  3. @kuldeepsidhu: In which format u want date to display….if u want to display date as 2010-10-08 then use this

    NSDate *date = [[NSDate alloc]init];
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@”yyyy-MM-dd”];
    NSString *dateString = [dateFormat stringFromDate:date];
    [dateFormat release];

  4. Hey Thanks…..
    Exactly what I want.. Really Good….

  5. Thanks . Really Good…. .plz post me link of ref.

  6. thaks, .. but i watn time format also .. iam getting the time format from webservice like this “0000” .Now i want to be done this format into “00:00”. How to do this. plz post me..

  7. How can I confirm whether an allowable date string i.e. xx/xx/xx or xx/xx/xxxx has been entered into a textfield?

    I take the string in code and convert it to a date and do math on the date. All is well if the date string is added properly as shown above. I have to validate the entry string and don’t know how.

    Thanks

  8. Hi
    Thanks for sharing. I’m having a problem though.
    I need to convert the following string into af date object: 2011-09-19T12:23:51Z

    And then convert the date object back to a string with this format: 19. september 2011

    I can’t figure out what the “T” and “Z” is all about?
    Can anyone help me?

    Kind regards
    Jesper

  9. Is this the right way to go?

    //——————————– CODE ———————————-

    NSString *dateStr = @”2011-09-19T12:23:51Z”;

    // Convert string to date objekt
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    dateFormat.dateFormat = @”yyyy-MM-ddThh:mm:ssZ”;
    NSDate *date = [dateFormat dateFromString:dateStr];

    // Convert date object to desired output format
    [dateFormat setDateFormat:@”dd. MMMM yyyy”];
    dateStr = [dateFormat stringFromDate:date];
    [dateFormat release];

    //—————————- CODE ENDS ——————————

  10. GOT IT!

    Needed to put ‘ ‘ around the ‘T’ and ‘Z’ like this:

    @”yyyy-MM-dd’T’HH:mm:ss’Z'”;

  11. Hi
    I have “2011-03-25 22:30:00 EST” that string . and i want convert it nsdate with 2011-03-25 22:30:00 +0000
    that formate so how
    i change please hlep

  12. hi i m usin following code

    NSString *dateStr =@”2011-09-19″;
    // Convert string to date objekt
    NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
    [dateFormat setDateFormat:@”dd.MM.yyyy”];
    NSDate *date = [dateFormat dateFromString:dateStr];
    // Convert date object to desired output format
    [dateFormat setDateFormat:@”dd.MM.yyyy”];
    dateStr = [dateFormat stringFromDate:date];
    NSLog(@”date===%@”,dateStr);

    but result of NSLog is ‘null’
    date===(null)

    what should i do?

    • When working with dates I’d recommend experimenting with various formats, it’s most likely the original date string or the format is not quite right.

  13. Thanks for the Code. This is filled exactly what I looking for.

Comments are closed.