Mobile App Developer
Mobile App Developer

Reputation: 91

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.

But still I have not getting success all the solutions are in ObjC I need to perform in Swift. Suppose my input is /Date(1434668400000+0100)/ for that expected output 19/06/2015.

I have tried below code and convert it from ObjC to Swift.

Objective-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. May be I am doing something wrong while conversion ObjC to Swift or may be something else.

any help will be highly appreciated.

Upvotes: 0

Views: 830

Answers (1)

Hamza Ansari
Hamza Ansari

Reputation: 3084

Check The Result generated from your code here:

enter image description here

Upvotes: 1

Related Questions