啥问题也有:小程序中web开发工具里显示时间正常,ios上显示的查8小时
原因: 使用 NSDate *date = [NSDate date]; 获取的时间是标注的UTC时间,和北京时间相差8小时.将UTC时间转成当地的时间只需要设置 NSTimeZone *zone = [NSTimeZone systemTimeZone];
1.给NSDate设置时区
NSDate *date = [NSDate date];
NSTimeZone *zone = [NSTimeZone systemTimeZone];
NSInteger interval = [zone secondsFromGMTForDate: date];
NSDate *localeDate = [date dateByAddingTimeInterval: interval];
2.给NSDateFormatter设置时区
NSDateFormatter *formater = [[NSDateFormatter alloc]init];
[formater setDateFormat:@"yyyy年MM月dd日"];
[formater setTimeZone:[NSTimeZone localTimeZone]];
NSDate *date = [formater dateFromString:dateStr];