Stefano Giuliani Stefano Giuliani - 1 year ago 86
iOS Question

Why NSDate from string has one day less?

if I try:

NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"yyyyMMdd"];


[formatter dateFromString:@"20120423"];

My new NSDate object will be: 2012 04 22. Why one day less?


Answer Source

You have problems with time zone and it can be solved by adding this line of code:

[formatter setTimeZone:[NSTimeZone timeZoneWithName:@"GMT"]];
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download