加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

objective-c – NSDate到GMTDate到JSON格式

发布时间:2020-12-16 09:39:23 所属栏目:百科 来源:网络整理
导读:我想将我的本地日期([NSDate日期])转换为GMT以创建 JSON字符串(/ Date(1324435876019-0000)/). 当我将时钟设置为EST时区时,我的代码工作正常,当我将时区更改为PST时,我的代码仍然充当EST.你能告诉我代码是什么问题吗? NSDate *localDate = [NSDate date]; N
我想将我的本地日期([NSDate日期])转换为GMT以创建 JSON字符串(/ Date(1324435876019-0000)/).

当我将时钟设置为EST时区时,我的代码工作正常,当我将时区更改为PST时,我的代码仍然充当EST.你能告诉我代码是什么问题吗?

NSDate *localDate = [NSDate date];
    NSTimeInterval timeZoneOffset = [[NSTimeZone systemTimeZone] secondsFromGMT]; // You could also use the systemTimeZone method
    NSTimeInterval gmtTimeInterval = [localDate timeIntervalSinceReferenceDate] - timeZoneOffset;
    NSDate *gmtDate = [NSDate dateWithTimeIntervalSinceReferenceDate:gmtTimeInterval];
    NSString *time = [NSString stringWithFormat:@"/Date(%@-0000)/",[NSNumber numberWithLongLong:[gmtDate timeIntervalSince1970] * 1000]];

解决方法

您只需调用timeIntervalSince1970即可获得时间GMT时间.

NSString *time = [NSString stringWithFormat:@"/Date(%lld-0000)/",(long long)([[NSDate date] timeIntervalSince1970] * 1000)];

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读