OC processing. Net JSON time format

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.