OC processing. Net Json time format, ocjson
The json time format received by the server is/Date (xxxxxxxxxxxxxxx + xxxx)/. The first half is the millionSecs from January 1, 1970, and the second half is the time zone. We need to align and convert.
There are two solutions. The first solution is to convert the time on the server to a string and then send the message. In this way, you can use NSDateFormater on IOS to obtain the time.
If you have no choice for the server, you have to switch it yourself.
As long as you understand the meaning of the xxxxxxxxxxxxxxx + xxxx format, we have some ideas.
First, use the regular expression to retrieve the time part and the time zone part.
The Code is as follows:
+ (NSDate *)DateFromDotNetJSONString:(NSString *)string { static NSRegularExpression *dateRegEx = nil; static dispatch_once_t onceToken; dispatch_once(&onceToken, ^{ dateRegEx = [[NSRegularExpression alloc] initWithPattern:@"^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$" options:NSRegularExpressionCaseInsensitive error:nil]; }); NSTextCheckingResult *regexResult = [dateRegEx firstMatchInString:string options:0 range:NSMakeRange(0, [string length])]; if (regexResult) { // milliseconds NSTimeInterval seconds = [[string substringWithRange:[regexResult rangeAtIndex:1]] doubleValue] / 1000.0; // timezone offset if ([regexResult rangeAtIndex:2].location != NSNotFound) { NSString *sign = [string substringWithRange:[regexResult rangeAtIndex:2]]; // hours seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:3]]] doubleValue] * 60.0 * 60.0; // minutes seconds += [[NSString stringWithFormat:@"%@%@", sign, [string substringWithRange:[regexResult rangeAtIndex:4]]] doubleValue] * 60.0; } return [NSDate dateWithTimeIntervalSince1970:seconds]; } return nil;}