简体   繁体   English

将.NET JSON日期转换为NSDate(Swift)

[英]Convert .NET JSON date to NSDate (Swift)

Hi I know that same question had been asked by several time and I have already gone through several links Link 1 , Link 2 , Link 3 and Link 4 and many more. 嗨,我知道相同的问题已经问了好几次了,我已经通过了几个链接Link 1Link 2Link 3Link 4等等。

But still I have not getting success all the solutions are in ObjC I need to perform in Swift. 但是我仍然没有获得成功,所有解决方案都在我需要在Swift中执行的ObjC中。 Suppose my input is /Date(1434668400000+0100)/ for that expected output 19/06/2015 . 假设我的输入是/ Date(1434668400000 + 0100)/对于该预期输出19/06/2015

I have tried below code and convert it from ObjC to Swift. 我试过下面的代码,并将其从ObjC转换为Swift。

Objective-C 目标C

+ (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;
}

Swift 迅速

func dateFromJSON(string: NSString) -> NSDate {
        var dateRegEx: NSRegularExpression!
        var onceToken: dispatch_once_t = 0

        dispatch_once(&onceToken) {
            dateRegEx = NSRegularExpression(pattern: "^\\/date\\((-?\\d++)(?:([+-])(\\d{2})(\\d{2}))?\\)\\/$", options: NSRegularExpressionOptions.CaseInsensitive, error: nil)!
        }
        var regexResult: NSTextCheckingResult = dateRegEx.firstMatchInString(string as String, options: NSMatchingOptions.allZeros, range: NSMakeRange(0, string.length))!

        var seconds: NSTimeInterval = (string.substringWithRange(regexResult.rangeAtIndex(1)) as NSString).doubleValue / 1000.0
        if regexResult.rangeAtIndex(2).location != NSNotFound {
            var sign: NSString = string.substringWithRange(regexResult.rangeAtIndex(2))
            seconds += NSString(format: "\(sign)", string.substringWithRange(regexResult.rangeAtIndex(3)) as NSString).doubleValue * 60.0*60.0
            seconds += NSString(format: "\(sign)", string.substringWithRange(regexResult.rangeAtIndex(4)) as NSString).doubleValue * 60.0*60.0

        }
        return NSDate(timeIntervalSince1970: seconds)
    }

When I called the function 当我调用函数

var date = dateFromJSON("/Date(1434668400000+0100)/")
println("Date: \(date)")

Output is 输出是

Date: 2015-06-18 23:00:00 +0000

I don't understand whats going on why I am getting 1 day difference. 我不明白为什么会出现1天的差异。 May be I am doing something wrong while conversion ObjC to Swift or may be something else. 将ObjC转换为Swift时,可能是我做错了,或者是其他事情。

any help will be highly appreciated. 任何帮助将不胜感激。

Check The Result generated from your code here: 在此处检查从您的代码生成的结果:

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM