Xcode 从WCF REST服务生成的JSON获取NSDate

Xcode 从WCF REST服务生成的JSON获取NSDate,xcode,nsdate,wcf-rest,epoch,Xcode,Nsdate,Wcf Rest,Epoch,下面的代码可以工作,但感觉脏。是否有更标准的方法将带有偏移量的历元日期转换为NSDate - (NSDate *) dateFromJSONString: (NSString *) JSONString{ //expects JSON from .NET WCF Service in epoch ticks, ex: //"timeScheduled":"\/Date(1348600140000+0100)\/" NSString *date = [[JSONString

下面的代码可以工作,但感觉脏。是否有更标准的方法将带有偏移量的历元日期转换为NSDate

- (NSDate *) dateFromJSONString: (NSString *) JSONString{
    //expects JSON from .NET WCF Service in epoch ticks, ex:
    //"timeScheduled":"\/Date(1348600140000+0100)\/"
    NSString *date = [[JSONString stringByReplacingOccurrencesOfString:@"/Date("         withString:@""] stringByReplacingOccurrencesOfString:@")/" withString:@""];
    NSString *offsetString = [date substringFromIndex:(date.length - 5)];

    //convert to seconds
    NSTimeInterval dateInterval = [date doubleValue] /1000;

    //gets offset value in seconds - +0100 -> 100 -> 1 -> 3600
    double offsetValue = ([offsetString doubleValue] / 100) * 60 * 60;
    if ([[offsetString substringToIndex:1] isEqualToString:@"+"]) {
        dateInterval = dateInterval + offsetValue;
    }
    else{
        dateInterval = dateInterval - offsetValue;
    }

    NSDate *retVal = [[NSDate alloc]initWithTimeIntervalSince1970:dateInterval];

    return retVal;
}
试试这个

///日期(-422928000000+0100)/ 文件:

DateTime值以“/Date(700000+0500)/”的形式显示为JSON字符串,其中第一个数字(提供的示例中为700000)是GMT时区的毫秒数,即自1970年1月1日午夜起的常规(非夏令时)时间。该数字可能为负数,以表示较早的时间。示例中由“+0500”组成的部分是可选的,指示时间是本地类型的-也就是说,应在反序列化时转换为本地时区。如果不存在,则将时间反序列化为Utc。忽略实际数字(本例中为“0500”)及其符号(+或-)

NSTimeInterval始终以秒为单位指定;它在10000年的范围内产生亚毫秒的精度

+ (NSDate*) dateFromDotNet:(NSString *)stringDate{
    if(stringDate==(id)[NSNull null])
        return nil;
    NSInteger ix0= [stringDate rangeOfString:@"("].location;
    NSInteger ix1= [stringDate rangeOfString:@")"].location;
    if(ix0==NSNotFound || ix1==NSNotFound)
        @throw [NSException exceptionWithName:@"ExceptionName" reason:@"Invalid JSON data" userInfo:@{@"json":stringDate}];
    NSRange range= NSMakeRange(ix0+1, ix1-ix0);
    NSString *dateString= [stringDate substringWithRange:range];
    // dateString:  -422928000000+0100    
    NSCharacterSet *signs= [NSCharacterSet characterSetWithCharactersInString:@"+-"];
    range= [dateString rangeOfCharacterFromSet:signs option:NSBackwardSearch];
    // 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
    NSTimeInterval unixTime = [dateString doubleValue] / 1000;

    if(range.location!=NSNotFound){
        NSString *sign = [dateString substringWithRange:range];
        NSString *off  = [dateString substringFromIndex:range.location+1];
        // gets offset value in seconds -+0100 -> 100 -> 1 -> 3600
        double offset  = ([off doubleValue] / 100) * 60 * 60;
        if ([sign isEqualToString:@"+"])
            unixTime+= offset;
        else
            unixTime-= offset;
    }
    NSDate *date= [NSDate dateWithTimeIntervalSince1970:unixTime];
    return date;
}

虽然此链接可以回答问题,但最好在此处包含答案的基本部分,并提供链接供参考。如果链接页面发生更改,仅链接的答案可能无效。这很公平。我有点简洁。:)你应该考虑增加一些解释。
+ (NSDate*) dateFromDotNet:(NSString *)stringDate{
    if(stringDate==(id)[NSNull null])
        return nil;
    NSInteger ix0= [stringDate rangeOfString:@"("].location;
    NSInteger ix1= [stringDate rangeOfString:@")"].location;
    if(ix0==NSNotFound || ix1==NSNotFound)
        @throw [NSException exceptionWithName:@"ExceptionName" reason:@"Invalid JSON data" userInfo:@{@"json":stringDate}];
    NSRange range= NSMakeRange(ix0+1, ix1-ix0);
    NSString *dateString= [stringDate substringWithRange:range];
    // dateString:  -422928000000+0100    
    NSCharacterSet *signs= [NSCharacterSet characterSetWithCharactersInString:@"+-"];
    range= [dateString rangeOfCharacterFromSet:signs option:NSBackwardSearch];
    // 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
    NSTimeInterval unixTime = [dateString doubleValue] / 1000;

    if(range.location!=NSNotFound){
        NSString *sign = [dateString substringWithRange:range];
        NSString *off  = [dateString substringFromIndex:range.location+1];
        // gets offset value in seconds -+0100 -> 100 -> 1 -> 3600
        double offset  = ([off doubleValue] / 100) * 60 * 60;
        if ([sign isEqualToString:@"+"])
            unixTime+= offset;
        else
            unixTime-= offset;
    }
    NSDate *date= [NSDate dateWithTimeIntervalSince1970:unixTime];
    return date;
}