Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/119.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/json/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
iOs中的JSON日期字符串转换问题_Ios_Json_Nsdate_Nsdateformatter - Fatal编程技术网

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

iOs中的JSON日期字符串转换问题,ios,json,nsdate,nsdateformatter,Ios,Json,Nsdate,Nsdateformatter,我从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 defau

我从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 GMT上午,但我应该得到2012-12-07 09:10:39(大约)

我该怎么做?请帮忙。

你可以试试这个

NSString   *jsonDateString = [SingleResponseObject objectForKey:@"datetime"];;
NSInteger offset = [[NSTimeZone defaultTimeZone] secondsFromGMT];
NSTimeInterval unixTime = [[jsonDateString substringWithRange:NSMakeRange(0, 13)] doubleValue] / 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]];
cell.date.text = stringFromDAte;
试试这个代码

- (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;
}
有关更多详细答案,请点击此链接

观察:如果要使用过去的日期,则使用NSMakeRange(0,13)是危险的。观察:不清楚传入的时间值是否经过时区调整。如果是时区调整(非UTC),则需要减去传入的时区偏移量。在服务器上,它获取当前时间并将其转换为UTC时间格式。
+0530
是两个日期之间的差异。这是有道理的。将偏移量转换为NSDate而不进行任何调整,然后NSLog它,以查看它转换为什么。