/ objective-c

Dealing with Dates & Time Zones in Objective-C

Date formatting is always one of those areas (especially in a newer language) where you continually need to look it up to do it the right way.

In an application that I'm building I have a need to deal with dates coming from an XML feed, so I need to parse them as a string. I also need to be able to display dates in a different format that I'm given (or translate them between various time zones) so clearly I can't just treat the value as a string.

For a complete reference check out the official documentation on NSDateFormatter.

String → Date

To convert from an NSString to NSDate, you have to create an `NSDateFormatter` and set the date format to a pattern matching your expected date. The format I was expected was like this:

`Sun, 20 Jun 2010 20:28:00 GMT`

In order to set up our format strings, we can reference the official Unicode standard date formatting tokens. For my needs, this turned out to be `EEE, dd MMM yyyy HH:mm:ss zzzz`. Plugging this into `NSDateFormatter` is pretty easy:

`Sun, 20 Jun 2010 20:28:00 GMT`
-(NSDate *)dateFromString:(NSString *)dateString {
NSDateFormatter *formatter = [[[NSDateFormatter alloc] init] autorelease];
[formatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss zzz"];
return [formatter dateFromString:dateString];

Sometimes we aren't lucky enough to have time zone information passed as part of the string. In that case, you may need to explicitly tell the formatter about your time zone:

[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];

Date → String

To convert from an NSDate to an NSString, it is quite similar to the method above, except your date format string is the format that you want your string to look like. In my case, I want a short date to be displayed in the corner of a `UITableViewCell`, so my formatting code looks like this:

NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"dd-MMM"];
cell.dateLabel.text = [formatter stringFromDate:item.pubDate];
[formatter release];

I chose to use a short word form for the month, because this app might be used by international folks and you might not be able to distinguish a date like `06/10`. You could of course examine the locale of the user and set the date format accordingly, but I think this is an easier way out :).

Date Format Symbols

The table from the unicode date formatting page should be enough for you to build your own desired date format string...

Pattern Result (in a particular locale)
yyyy.MM.dd G 'at' HH:mm:ss zzz 1996.07.10 AD at 15:08:56 PDT
EEE, MMM d, ''yy Wed, July 10, '96
h:mm a 12:08 PM
hh 'o''clock' a, zzzz 12 o'clock PM, Pacific Daylight Time
K:mm a, z 0:00 PM, PST
yyyyy.MMMM.dd GGG hh:mm aaa 01996.July.10 AD 12:08 PM

Hope this is useful to someone out there.