gpt4 book ai didi

ios - iOs 中的 JSON 日期字符串转换问题

转载 作者:塔克拉玛干 更新时间:2023-11-02 21:46:45 26 4
gpt4 key购买 nike

我从 WCF Rest 服务获取一些 UTC 日期字符串,格式如下:

/Date(1354851639500+0530)/

我使用下面的代码来转换日期:

//jsonDateString = 1354851639500+0530

NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; //get number of seconds to add or subtract according to the client default time zone

NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(0, 13)] doubleValue] / 1000; //WCF will send 13 digit-long value for the time interval since 1970 (millisecond precision) whereas iOS works with 10 digit-long values (second precision), hence the divide by 1000

NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
NSTimeZone *timeZone = [NSTimeZone timeZoneWithName:@"UTC"];
[dateFormatter setTimeZone:timeZone];
[dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss a ZZZZ"];
NSString *stringFromDAte = [dateFormatter stringFromDate:[[NSDate dateWithTimeIntervalSince1970:unixTime] dateByAddingTimeInterval:offset]];
NSLog(@"Server GMT: %@", stringFromDAte);


NSDate *currentDadte = [dateFormatter dateFromString:stringFromDAte];
NSTimeInterval interval = [currentDadte timeIntervalSinceDate:[NSDate date]];

return [self dailyLanguage:interval];

但是我转换的时候不正确。我需要获取接收时间的 UTC 时间。但是我得到的是没有偏移值的时间值。

例如:如果 josnDate = 1354851639500+0530,我得到,2012-12-07 03:40:39 AM GMT,但我应该得到 2012-12-07 09:10:39(大约)。

我该怎么做?请帮忙。

最佳答案

试试这个代码

- (NSDate *)deserializeJsonDateString: (NSString *)jsonDateString
{
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT]; //get number of seconds to add or subtract according to the client default time zone

NSInteger startPosition = [jsonDateString rangeOfString:@"("].location + 1; //start of the date value

NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(startPosition, 13)] doubleValue] / 1000; //WCF will send 13 digit-long value for the time interval since 1970 (millisecond precision) whereas iOS works with 10 digit-long values (second precision), hence the divide by 1000

[[NSDate dateWithTimeIntervalSince1970:unixTime] dateByAddingTimeInterval:offset];

return date;
}

有关更多详细信息,请点击此链接 http://goo.gl/lE6ut

关于ios - iOs 中的 JSON 日期字符串转换问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13763343/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com