Reputation: 827
What is the appropriate way to format this string:
2012-07-28T23:59:00-06:00
This is the code I am using and the date formatter is currently setting the value of s.due_At to nil and both NSLogs are displaying null. I am getting the string above back from the dictionary for sure I have NSLogged it.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HH:mm:ssZ"];
s.due_At = [dateFormatter dateFromString:[assignmentData objectForKey:@"due_at"]];
NSLog(@"%@",[dateFormatter stringFromDate:s.due_At]);
NSLog(@"%@",[dateFormatter dateFromString:[assignmentData objectForKey:@"due_at"]]);
How can I fix this to output the above string?
Upvotes: 0
Views: 212
Reputation: 827
Thanks for your help. Ok so I changed the formatter a bit. Here is the code.
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateStyle:NSDateFormatterShortStyle];
[dateFormatter setLocale:[NSLocale currentLocale]];
[dateFormatter setDateFormat:@"yyyy-MM-dd'T'HHmmssZ"];
s.due_At = [dateFormatter dateFromString:[[assignmentData objectForKey:@"due_at"] stringByReplacingOccurrencesOfString:@":" withString:@""]];
NSLog(@"%@",[dateFormatter stringFromDate:s.due_At]);
The Nslog is no longer null although it still contains the T in the middle. This is the log message now.
2012-07-29T015900-0400
Upvotes: 0
Reputation: 8944
Z
literal means RFC 822 standard which does not allow you to have :
between hours and minutes:
zone = "UT" / "GMT" ; Universal Time
; North American : UT
/ "EST" / "EDT" ; Eastern: - 5/ - 4
/ "CST" / "CDT" ; Central: - 6/ - 5
/ "MST" / "MDT" ; Mountain: - 7/ - 6
/ "PST" / "PDT" ; Pacific: - 8/ - 7
/ 1ALPHA ; Military: Z = UT;
; A:-1; (J not used)
; M:-12; N:+1; Y:+12
/ ( ("+" / "-") 4DIGIT ) ; Local differential
; hours+min. (HHMM)
Upvotes: 1