需要以下代码的帮助:
self.dateTimestamp = userInfo[@"timestamp"];<- Returns UTC Time April 09 2018 11:09
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM dd YYYY HH:mm"];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setCalendar:[NSCalendar currentCalendar]];
NSDate *dte = [dateFormat dateFromString:self.dateTimestamp];
NSLog(@"Date: %@", dte); <-Date: Sun Dec 24 11:09:00 2017
为什么我有日期差异。
答案 0 :(得分:2)
只需将YYYY
替换为yyyy
:
NSString *dateTimestamp = @"April 09 2018 13:07";
NSDateFormatter *dateFormat = [[NSDateFormatter alloc] init];
[dateFormat setDateFormat:@"MMMM dd yyyy HH:mm"];
[dateFormat setTimeZone:[NSTimeZone systemTimeZone]];
[dateFormat setCalendar:[NSCalendar currentCalendar]];
NSDate *dte = [dateFormat dateFromString:dateTimestamp];
NSLog(@"Date: %@", dte);
结果:Mon Apr 9 13:07:00 2018
但使用时
[dateFormat setDateFormat:@"MMMM dd YYYY HH:mm"];
正在记录:Sun Dec 24 13:07:00 2017