How to Convert an ISO8601 Date in iOS

I still find working with dates in iOS a little tricky. There is always a new format to deal with and it’s all about getting the identifiers for the format just right! I recently ran into ISO8601 and hope the bit of code below is of some use.

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZZZZZ"];
NSDate *date = [dateFormatter dateFromString:self.releaseDate];
[dateFormatter setDateStyle:NSDateFormatterLongStyle];
self.dateString = [dateFormatter stringFromDate:date];

You can, of course, pick any other date format style (instead of Long) as the output. As always, let me know if you have any questions.

2 thoughts on “How to Convert an ISO8601 Date in iOS

  1. Norm Post author

    Hi Ronit, sorry for the delay.

    ISO8601 has several acceptable representations for (human readable) times. It does use a 24 hour clock and requires zero-padded values (e.g. 8am is 08:00:00). Both of the human readable formats you list above are acceptable ISO8601 formats, so I’m not exactly sure what you are asking. As far as internet standards go, ISO8601 is implemented via RFC3339 , and this also true for iOS.

    Perhaps if you could provide a little more insight into what you are trying to accomplish, I could provide a little more direction. If you are dealing just with times, I find it’s easies to turn everything into “total seconds” do all my calculations using that value, and then only convert back into some human readable time at the end (for display). In this case, I don’t actually use a “date” object.

    Reply

Leave a Reply

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