title: Convert .NET JSON date to NSDate (Swift)
body: <p>Hi I know that same question had been asked by several time and I have already gone through several links <a href="https://stackoverflow.com/questions/5810033/formatting-a-datetime-object-from-net-into-a-nsdate-for-objective-c">Link 1</a>, <a href="https://stackoverflow.com/questions/1757303/parsing-json-dates-on-iphone">Link 2</a>, <a href="http://zdravevski.com/blog/2012/05/27/json-serialized-date-passed-between-ios-and-wcf-and-vice-versa/" rel="nofollow noreferrer">Link 3</a> and <a href="http://simianzombie.com/?p=2352" rel="nofollow noreferrer">Link 4</a> and many more.</p>
  
  <p>But still I have not getting success all the solutions are in ObjC I need to perform in Swift. Suppose my input is <strong>/Date(1434668400000+0100)/</strong> for that expected output <strong>19/06/2015</strong>.</p>
  
  <p>I have tried below code and convert it from ObjC to Swift.</p>
  
  <p><strong>Objective-C</strong></p>
  
  <pre><code>+ (NSDate *)mfDateFromDotNetJSONString:(NSString *)string {
      static NSRegularExpression *dateRegEx = nil;
      static dispatch_once_t onceToken;
      dispatch_once(&amp;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;
  }
  </code></pre>
  
  <p><strong>Swift</strong></p>
  
  <pre><code>func dateFromJSON(string: NSString) -&gt; NSDate {
          var dateRegEx: NSRegularExpression!
          var onceToken: dispatch_once_t = 0
  
          dispatch_once(&amp;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)
      }
  </code></pre>
  
  <p>When I called the function </p>
  
  <pre><code>var date = dateFromJSON("/Date(1434668400000+0100)/")
  println("Date: \(date)")
  </code></pre>
  
  <p>Output is </p>
  
  <pre><code>Date: 2015-06-18 23:00:00 +0000
  </code></pre>
  
  <p>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.</p>
  
  <p>any help will be highly appreciated.</p>