Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why NSDate from string has one day less?

if I try:

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

and

[formatter dateFromString:@"20120423"];

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

Thank

like image 654
Stefano Giuliani Avatar asked Apr 23 '12 10:04

Stefano Giuliani


2 Answers

For those who looks for swift 3 version

let formatter        = DateFormatter()
formatter.timezone   = TimeZone(abbreviation: "GMT")
formatter.dateFormat = "yyyyMMdd"
formatter.date(from: "20120423")
like image 155
Anand Avatar answered Oct 19 '22 17:10

Anand


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

formatter.timeZone = [NSTimeZone timeZoneWithName:@"GMT"];
like image 22
Justin Boo Avatar answered Oct 19 '22 18:10

Justin Boo