Reputation: 2675
I'm using the NSDateFormatter and it gives me strange result
NSString *startDateStr = @"2014-05-17 11:00 AM";
NSString *endDateStr = @"2014-05-17 11:30 AM"
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyy-MM-dd HH:mm a"];
startDate = [formatter dateFromString:startDateStr];
endDate = [formatter dateFromString:endDateStr];
No matter if i use
[formatter setTimeZone:[NSTimeZone localTimeZone]];
Or not.. it gives me a result
2014-05-16 21:00:00 +0000
For startDate And
2014-05-16 21:30:00 +0000 For endDate
I have no clue for what it could be the problem!
Upvotes: 0
Views: 289
Reputation: 52538
I suppose you are living somewhere near Australia?
You need to realise what NSDate does and displays. NSDate displays the time in GMT. So if you convert 11am with your local time zone, and at the same time it is 9pm in GMT (which is Greenwich, near London, when they don't have daylight savings time), then NSDate will display 9pm. You absolutely have the correct date and time, you just need to get used to the way how NSDate displays it.
If someone in New York ran your code, they would get a different result, because "11 am local time" in New York is not the same time as "11 am local time" where you live.
Setting [NSTimeZone localTimeZone] doesn't make a difference because that's the default for a date formatter.
Upvotes: 1