2

jsonファイルから以下の形式で日付を受け取ります。よくわかりませんが、.NetフレームワークのDataContractJsonSerializerクラスでフォーマットされていると思います。日付はこんな感じ

    \/Date(1255993200000+0100)\/

iOSでこれを通常の日付に変換する方法を誰かが知っているのか、それとも変更するために何かをしなければならないのか、疑問に思っていました。

ありがとう、

4

1 に答える 1

6

これを試して

-(NSDate*)mfDateFromDotNetJSONString:(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;
}

を使用して文字列を渡します

[NSString stringWithFormat:...]この機能に

これは私にとって助けになり、あなたに役立つことを願っています

于 2012-07-02T14:34:51.903 に答える