【发布时间】:2014-07-06 23:16:41
【问题描述】:
我正在尝试从设备获取本地时间
nsdate 将返回 UTC 时间
当我使用本地时区转换时,字符串将返回当前本地时间...
我再次尝试将字符串转换为本地日期,但它只会返回 UTC 时间
这是我的代码...如何在 NSDate 中获取当地时间...
NSDate *currentDate = [NSDate date];
NSLog(@"currentDate: %@", currentDate); // Result: currentDate: 2014-05-19 05:21:50 +0000
NSDateFormatter *dateFormatter2 = [[NSDateFormatter alloc] init];
[dateFormatter2 setTimeZone:[NSTimeZone localTimeZone]];
[dateFormatter2 setDateFormat:@"yyyy/MM/dd HH:mm:ss"];
NSString *strDate = [dateFormatter2 stringFromDate:currentDate];
NSLog(@"strDate: %@", strDate); // Result: strDate: 2014/05/19 10:51:50
NSDate *newDate = [dateFormatter2 dateFromString:strDate];
NSLog(@"newDate: %@", newDate); // Result: newDate: 2014-05-19 05:21:50 +0000
任何人都可以帮助在 NSDate 中获取本地日期时间
【问题讨论】:
-
OP 声明:“当我使用本地时区转换时,字符串将返回当前本地时间”所以没有问题。
标签: ios nsdate nsdateformatter