Date Formatter Examples – Take 3: Date from String

Wed, Nov 26

1
2
3
4
5
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
NSString *dateString = [dateFormat stringFromDate:date];  
[dateFormat release];

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:

1
2
3
4
5
6
7
8
9
10
11
NSString *dateStr = @"20081122";
 
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];  
 
// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];  
[dateFormat release];

More on Date Formatting:

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

-->

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

1
2
3
4
5
NSDate *date = [NSDate date];
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
NSString *dateString = [dateFormat stringFromDate:date];  
[dateFormat release];

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:

1
2
3
4
5
6
7
8
9
10
11
NSString *dateStr = @"20081122";
 
// Convert string to date object
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"yyyyMMdd"];
NSDate *date = [dateFormat dateFromString:dateStr];  
 
// Convert date object to desired output format
[dateFormat setDateFormat:@"EEEE MMMM d, YYYY"];
dateStr = [dateFormat stringFromDate:date];  
[dateFormat release];

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

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

by Ilia Baskin on Mar 2, 2010. #

Nice post it helped me

by Muthukumar on Mar 30, 2010. #

Thanks! Simple, Fast, Useful… 8)

by Roman on Jun 21, 2010. #

Sweet! That is exactly what I wanted, thanks!

by Martin Eigo on Jul 31, 2010. #

Thanks a ton…! It is really helpful..:)

by Harinder on Aug 5, 2010. #

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..?

by kuldeepsidhu on Oct 8, 2010. #

it helped me a lot…..short & easy…thank u..

by sonali on Oct 18, 2010. #

cool……….

by kdgf on Jun 27, 2011. #

@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];

by sonali on Oct 19, 2010. #

@sonali….thank you…it works…

by kuldeepsidhu on Oct 19, 2010. #

Great, just what i need.

by Andrew on Jan 3, 2011. #

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

by Mangesh vyas on Jan 7, 2011. #

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

by saravanan on Jan 27, 2011. #

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..

by narayanarao on Apr 28, 2011. #

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

by Glenn Tisman on Sep 10, 2011. #

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

by Jesper on Sep 21, 2011. #

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 ——————————

by Jesper on Sep 21, 2011. #

GOT IT!

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

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

by Jesper on Sep 21, 2011. #

7th, sept 2011 how to get this format?

by saravanan on Dec 14, 2011. #

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

by RBNiranjan on Mar 19, 2012. #

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?

by Irfan on Jul 10, 2012. #

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.

by John Muchow on Jul 11, 2012. #

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

by Bandaram Parashuram on Nov 20, 2012. #

This really helped me out. Thanks a lot!!!

by Amit on Feb 27, 2014. #