微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

objective-c – 当前日期和时间 – NSDate

我需要显示当前的日期和时间.

我用过 ;

NSDate *currentDateNTime        = [NSDate date];

我想拥有当前的日期和时间(应该显示系统时间而不是GMT时间).
输出应该是NSDate格式而不是Nsstring.

例如;

NSDate *currentDateNTime        = [NSDate date];
// Do the processing....

NSDate *NowDateAndTime = .....; // Output should be a NSDate and not a Nsstring

解决方法

由于所有NSDate都是GMT引用,您可能想要这样:
(不要忘记NowDate不会是实际的当前系统日期时间,但它会“移位”,所以如果你使用NSDateFormatter生成Nsstring,你会看到错误的日期)
NSDate* currentDate = [NSDate date];
NSTimeZone* currentTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* NowTimeZone = [NSTimeZone systemTimeZone];

NSInteger currentGMTOffset = [currentTimeZone secondsFromGMTForDate:currentDate];
NSInteger NowGMTOffset = [NowTimeZone secondsFromGMTForDate:currentDate];

NSTimeInterval interval = NowGMTOffset - currentGMTOffset;
NSDate* NowDate = [[NSDate alloc] initWithTimeInterval:interval sinceDate:currentDate];

原文地址:https://www.jb51.cc/c/111296.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐