Parsing and Formatting Dates

Episode #99 | 14 minutes | published on 12/12/2013
Free Video
In this episode we cover a basic but fundamentally important topic: dealing with dates & times. Particularly when dealing with an API, dates can arrive in all shapes and sizes. We'll examine some of the common ones, talk about how to parse these formats into NSDate instances, and how to use NSDateFormatter to display them back again as a string.

Episode Links

Common Date Formats

The following table's sample column are mostly based on the time December 14th, 2008 4:35 PM UTC.

Character Example Description
yy 08 2-digit year
yyyy 2008 4-digit year
Q 4 The quarter of the year. Use QQ if you want zero padding.
QQQ Q4 Quarter including "Q"
QQQQ 4th quarter Quarter spelled out
M 12 The numeric month of the year. A single M will use '1' for January.
MM 12 The numeric month of the year. A double M will use '01' for January.
MMM Dec The shorthand name of the month
MMMM December Full name of the month
MMMMM D Narrow name of the month
d 14 The day of the month. A single d will use 1 for January 1st.
dd 14 The day of the month. A double d will use 01 for January 1st.
F 3rd Tuesday in December The day of week in the month
E Tues The day of week in the month
EEEE Tuesday The full name of the day
EEEEE T The narrow day of week
h 4 The 12-hour hour.
hh 04 The 12-hour hour padding with a zero if there is only 1 digit
H 16 The 24-hour hour.
HH 16 The 24-hour hour padding with a zero if there is only 1 digit.
a PM AM / PM for 12-hour time formats
m 35 The minute, with no padding for zeroes.
mm 35 The minute with zero padding.
s 8 The seconds, with no padding for zeroes.
ss 08 The seconds with zero padding.
Time Zone
zzz CST The 3 letter name of the time zone. Falls back to GMT-08:00 (hour offset) if the name is not known.
zzzz Central Standard Time The expanded time zone name, falls back to GMT-08:00 (hour offset) if name is not known.
zzzz CST-06:00 Time zone with abbreviation and offset
Z -0600 RFC 822 GMT format. Can also match a literal Z for Zulu (UTC) time.
ZZZZZ -06:00 ISO 8601 time zone format

Episode Source Code

Update - Setting the Locale recommended

Setting the locale is strongly recommended to avoid issues parsing dates in various regions. For some background on why, read this Tech note from Apple.

Parsing dates in ISO 8601 Date format:

NSString *date1Str = @"2013-12-10T22:23:31Z";
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
NSLocale enUSPosixLocale = [NSLocale localeWithLocaleIdentifier:@"en_US_POSIX"];
[formatter setLocale:enUSPosixLocale];
[formatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
NSDate *date = [formatter dateFromString:date1Str];

Presenting dates for display:

NSDateFormatter *displayFormatter = [[NSDateFormatter alloc] init];
[displayFormatter setDateStyle:NSDateFormatterShortStyle];

NSLog(@"%@", [displayFormatter stringFromDate:date]);  // ==>  12/10/13  in en_US locale

[displayFormatter setLocale:[NSLocale localeWithLocaleIdentifier:@"en_GB"]];
NSLog(@"%@", [displayFormatter stringFromDate:date]);  // ==>  10/12/2013 (note the 4-digit year)

Displaying Times:

[displayFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"PST"]];
[displayFormatter setDateFormat:@"h:mm a ZZZ"];

NSLog(@"The time is %@", [displayFormatter stringFromDate:date]); // ==> 8:23 PM PST

Note that locale and time zone should generally be left at the device's settings, but there are cases where you'll need to explicitly set them.

blog comments powered by Disqus