NSDate *date = [NSDate date];//零时区的时间(格林尼治)
本机的时间 减去 本机时区的固定差值算出的UTC时间。所以手动改手机时间的话,结果也会变
// NSDate *nowDate = [[NSDate alloc] initWithTimeIntervalSinceNow:28800];//北京时间
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
[dateFormatter setTimeZone:[NSTimeZone timeZoneWithName:@"Asia/Shanghai"]];东八区
NSString *nowDateStr = [dateFormatter stringFromDate:nowDate];
NSString *dateStr = [dateFormatter stringFromDate:date];////北京时间字符串
yyyy-MM-dd HH:mm:ss:使用24小时制格式化日期
yyyy-MM-dd hh:mm:ss:使用12小时制格式化日期
使用NSDateFormatter转换时间字符串时,默认的时区是系统时区,例如在中国一般都是北京时间(+8),如果直接转换会导致结果相差8小时,所以一般的做法是先指定时区为GMT标准时间再转换,
网友评论