NSDate到GMTDate到JSON格式

AAV*_*AAV 3 objective-c nsdate nstimezone nstimeinterval ios

我想将我的本地日期([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]];
Run Code Online (Sandbox Code Playgroud)

Joe*_*Joe 7

GMT只需拨打电话即可获得纪元时间timeIntervalSince1970.

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