在y应用程序中,用户可以从不同时区发布帖子,我的任务是向他们显示发布帖子后多少分钟或几小时。但目前只能正确显示我的时区,而不适用于其他时区。如何在ios中显示不同时区的相同时差
是我到目前为止执行:
NSTimeInterval timestamp = [data.createdDate longLongValue];
NSDate *date = [NSDate dateWithTimeIntervalSince1970:timestamp];
NSTimeZone* sourceTimeZone = [NSTimeZone timeZoneWithAbbreviation:@"GMT"];
NSTimeZone* destinationTimeZone = [NSTimeZone systemTimeZone];
NSInteger sourceGMTOffset = [sourceTimeZone secondsFromGMTForDate:date];
NSInteger destinationGMTOffset = [destinationTimeZone secondsFromGMTForDate:date];
NSTimeInterval interval = destinationGMTOffset - sourceGMTOffset;
date = [[NSDate alloc] initWithTimeInterval:interval sinceDate:date];
date.createdDate是时间戳值从服务器的到来。 服务器旁边的时间是我的时区。
您有在其他时区2时区,这就是为什么有差别的日期。为什么不在两个日期中使用相同的时区? – rishi
我不明白这是怎么工作的! @rishi – Fay007
目前发生的情况是,您的源时区始终为GMT,并且目标时区正在根据设备设置进行更改,但当目标时区不同时,它将有不同的时间。您可以尝试将两个时区都设置为systemTimeZone,即将日期从服务器转换为系统时区,然后获取时差。 – rishi