Houman
Houman

Reputation: 66320

How to parse the timezone in this date string?

I would like to convert this string to NSDate: "Sun, 08 Mar 2015 10:32:12 -0000"

This is what I got working:

NSDateFormatter *dateformatter = [NSDateFormatter new];
[dateformatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss"];
NSDate *date = [dateformatter dateFromString:@"Sun, 08 Mar 2015 10:32:12"];

But I don't know how to parse the -0000, if its part of the string, the date would fail.

Upvotes: 0

Views: 209

Answers (1)

chrissukhram
chrissukhram

Reputation: 2967

You need to add a 'Z' to handle the timezone piece of the date string.

Example:

NSDateFormatter *dateformatter = [NSDateFormatter new];
[dateformatter setDateFormat:@"EEE, dd MMM yyyy HH:mm:ss Z"];
NSDate *date = [dateformatter dateFromString:@"Sun, 08 Mar 2015 10:32:12 -0000"];

Upvotes: 1

Related Questions