Reputation: 2756
Maybe it's an old question and I've searched on this site and found some similar questions, but I still cannot solve my problem. I have a NSString named gameDateTimeStr: "11/12/2013-10:00 PM" and I want to convert it to NSDate. I used the following code:
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"MM/dd/yyyy-HH:mm a"];
[formatter setLocale:[NSLocale currentLocale]];
NSDate *gameDateTime = [formatter dateFromString:gameDateTimeStr];
However, when printed on consolte, its description is: 2013-11-12 05:00:00 +0000
As you can see, the date is correct, but the time is wrong.
Please help me fix it.
Upvotes: 0
Views: 2526
Reputation: 69469
There are one issues with you date formatter, first you are using 24 hour format, HH
for the hours in the date format. But you example uses 12 hour format and hh
should be used.
[formatter setDateFormat:@"MM/dd/yyyy-hh:mm a"];
Depending on you timezone offset the date is parsed correctly, since date printedby NSLog
is is represented in GMT.
When you create a string form the date object use NSDateFormatter
you timezone is used to calculate the correct time offset.
Upvotes: 5