iPhone:NSDate 将 GMT 转换为当地时间

发布于 2024-09-27 00:45:25 字数 140 浏览 2 评论 0原文

目前,我有一个 API 调用返回一个日期/时间,格式如下:2010-10-10T07:54:01.878926

我可以假设这是 GMT 吗?我还将其转换为 NSDate 对象。有没有办法使用iPhone本地时间重新计算时间?

I currently have an API call returning me a date/time in the format: 2010-10-10T07:54:01.878926

Can I assume this is GMT? I also converted this to an NSDate object. Is there a way to use the local iPhone time to recalculate the time?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

撑一把青伞 2024-10-04 00:45:25
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";

NSTimeZone *gmt = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
[dateFormatter setTimeZone:gmt];
NSString *timeStamp = [dateFormatter stringFromDate:[NSDate date]];
[dateFormatter release];
硬不硬你别怂 2024-10-04 00:45:25

此代码会将 GMT 时间转换为设备的本地时间。

NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:pubDate];

This code will convert the GMT time to the device's local time.

NSDate* localDateTime = [NSDate dateWithTimeInterval:[[NSTimeZone systemTimeZone] secondsFromGMT] sinceDate:pubDate];
二智少女猫性小仙女 2024-10-04 00:45:25
NSDate *sourceDate = [NSDate dateWithTimeIntervalSince1970:gmtTimestamp];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [destinationTimeZone secondsFromGMTForDate:0];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
[dateFormatter setTimeZone:destinationTimeZone];
NSString *localTimeString = [dateFormatter stringFromDate:destinationDate];
NSDate *sourceDate = [NSDate dateWithTimeIntervalSince1970:gmtTimestamp];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [destinationTimeZone secondsFromGMTForDate:0];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:sourceDate];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
NSDate* destinationDate = [[[NSDate alloc] initWithTimeInterval:interval sinceDate:sourceDate] autorelease];
NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
dateFormatter.dateFormat = @"yyyy-MM-dd'T'HH:mm";
[dateFormatter setTimeZone:destinationTimeZone];
NSString *localTimeString = [dateFormatter stringFromDate:destinationDate];
ぇ气 2024-10-04 00:45:25
NSDate *now = [NSDate date];
NSLog(@"%@", [now dateWithCalendarFormat:@"%Y-%m-%d %H:%M:%S" 
              timeZone:[NSTimeZone timeZoneWithName:@"GMT"]]);
NSDate *now = [NSDate date];
NSLog(@"%@", [now dateWithCalendarFormat:@"%Y-%m-%d %H:%M:%S" 
              timeZone:[NSTimeZone timeZoneWithName:@"GMT"]]);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文