Date Formatters – Part 3

Fri, Jan 16

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:

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

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

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:

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

2 comments

thank u for this coding

by Anonymous on Jul 10, 2010. Reply #

Thanks for the sample, very useful!!!

i will be trying to add the time into mine as well, will post it once its done!

by Iain on Sep 14, 2010. Reply #

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>