获取正确的 NSDate 值 - 这是最有效的方法吗?
我有这个代码来获取本地日期/时间。它有效,但要获取我当前的日期/时间值而不是 GMT 时间,似乎还有很长的路要走(10 个语句)。
NSDate *currentDateGMT = [NSDate date];
NSDateFormatter *currentDateDateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *currentDateTimeZoneGMT = [NSTimeZone timeZoneWithName:@"GMT"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[currentDateDateFormatter setTimeZone: currentDateTimeZoneGMT];
[currentDateDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
[currentDateDateFormatter setLocale:locale];
NSString *currentDateString = [currentDateDateFormatter stringFromDate:currentDateGMT];
NSDate *currentDateAdjusted = [currentDateDateFormatter dateFromString:currentDateString];
[currentDateDateFormatter release];
有人可以确认这是获取当前机器价值的最佳方法吗?
谢谢
I have this code to get the local date/time. It works but it seems a long way around the bush (10 statements) to get my current date/time value rather than the GMT time.
NSDate *currentDateGMT = [NSDate date];
NSDateFormatter *currentDateDateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *currentDateTimeZoneGMT = [NSTimeZone timeZoneWithName:@"GMT"];
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:@"en_US_POSIX"];
[currentDateDateFormatter setTimeZone: currentDateTimeZoneGMT];
[currentDateDateFormatter setDateFormat:@"yyyy-MM-dd HH:mm"];
[currentDateDateFormatter setLocale:locale];
NSString *currentDateString = [currentDateDateFormatter stringFromDate:currentDateGMT];
NSDate *currentDateAdjusted = [currentDateDateFormatter dateFromString:currentDateString];
[currentDateDateFormatter release];
Can someone confirm that this is the best way to obtain the current machine value?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
NSDateFormatter
将默认为用户时区,因此最简单的解决方案是NSDateFormatter
will default to the users time zone so the simplest solution is