d3p0nit
d3p0nit

Reputation: 442

Correct Format for String to Date?

I m sorry stackoverflow is full of dateformatter questions but i m having a really hard time to get a string into a NSDate.

This is the String i receive:

2014-12-22T06:49:40+0000

And this is how i m trying to format it and get it to a NSDate:

NSString *time = @"2014-12-22T06:49:40+0000";
NSDateFormatter *df = [[NSDateFormatter alloc] init];
[df setDateFormat:@"yyyy-MM-dd'T'hh:mm:ss'Z'"];
[df setTimeZone:[NSTimeZone systemTimeZone]];
NSDate *myDate = [df dateFromString:time];

Unfortunately myDate is always nil. Am i missing somtheing or is my formatting simply wrong?

Thanks in advance

Upvotes: 0

Views: 47

Answers (2)

Dave DeLong
Dave DeLong

Reputation: 243146

You've wrapped your final Z in single quotes, which means you want it to be a literal Z character, but no such character exists in your time string. Removing the single quotes means it would be looking for a timezone offset, like you want.

Upvotes: 1

Owen Hartnett
Owen Hartnett

Reputation: 5935

Try this:"yyyy'-'MM'-'dd'T'HH':'mm':'ss'.'SSSSSSSZ"

Upvotes: 1

Related Questions