代碼:
NSString *ds = @"2013-02-25";
NSDateFormatter* formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:@"YYYY-MM-dd"];
NSDate* date = [formatter dateFromString:ds];
NSLog(@"%@", date); //2012-02-24 16:00:00 +0000
[formatter setDateFormat:@"MMM dd, YYYY"];
NSLog(@"%@", [formatter stringFromDate:date]); //Feb 25, 2012
為什麼會返回一年前的日期?怎麼修改,請不吝賜教,謝謝。
將下面的代碼換掉
[formatter setDateFormat:@"YYYY-MM-dd"];
[formatter setDateFormat:@"MMM dd, YYYY"];
換成:
[formatter setDateFormat:@"yyyy-MM-dd"];
[formatter setDateFormat:@"MMM dd, yyyy"];