The JSON time format received by the server is/date (xxxxxxxxxxxxx+xxxx)/, where the first half is millionsecs from 1970, the second half is the time zone, and we need to align to convert.
There are two ways to solve the problem, the first is to convert the time to a string in the server and then send it, so you can get the time directly with Nsdateformater on the iOS side.
If you have no choice on the service side, you will have to change the
As long as we understand the meaning of the xxxxxxxxxxxxx+xxxx format, we have ideas.
First, the time part and the time zone part are taken out by regular expression, and the calculation is possible.
The code is as follows
+ (NSDate *) datefromdotnetjsonstring: (NSString *)string { StaticNsregularexpression *dateregex =Nil; Staticdispatch_once_t Oncetoken; Dispatch_once (&oncetoken, ^{Dateregex= [[Nsregularexpression alloc] Initwithpattern:@"^\\/date\\ ((-?\\d++) (?:( [+-]) (\\d{2}) (\\d{2}))? \\)\\/$"options:nsregularexpressioncaseinsensitive Error:nil]; }); Nstextcheckingresult*regexresult = [Dateregex firstmatchinstring:stringOptions0Range:nsmakerange (0, [stringlength]); if(regexresult) {//millisecondsNstimeinterval seconds = [[stringSubstringwithrange:[regexresult Rangeatindex:1]] [Doublevalue]/1000.0; //TimeZone offset if([Regexresult Rangeatindex:2].location! =nsnotfound) {NSString*sign = [stringSubstringwithrange:[regexresult Rangeatindex:2]]; //hoursSeconds + = [[NSString stringWithFormat:@"%@%@", sign, [stringSubstringwithrange:[regexresult Rangeatindex:3]] [Doublevalue] *60.0*60.0; //minutesSeconds + = [[NSString stringWithFormat:@"%@%@", sign, [stringSubstringwithrange:[regexresult Rangeatindex:4]] [Doublevalue] *60.0; } return[NSDate datewithtimeintervalsince1970:seconds]; } returnNil;}
OC processing. Net JSON time format