mokacoding

unit and acceptance testing, automation, productivity

NSDateFormatter format for JSON dates

I always have to look this up, so I though I should write a little memo post about it 😊.

As mobile developers we often have to work with JSON APIs, and they usually have dates.

JSON itself doesn't have a date type, so dates are represented as strings. The standard way to formate date strings in JSON is to use the same format as Javascript's Date toJSON method, whic is ISO 8601 compliant.

2016-05-11T19:02:16.238Z

Here's how to configure NSDateFormatter to handle this date format:

import Foundation

extension NSDateFormatter {

  static func formatterForJSONDate() -> NSDateFormatter {
    let formatter = NSDateFormatter()
    formatter.dateFormat = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"
    formatter.timeZone = NSTimeZone(forSecondsFromGMT: 0)
    formatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
    return formatter
  }
}

And here's a test for it:

func testProperty() {
    let now = NSDate()
    let nowToJSON = sut.stringFromDate(now)
    XCTAssertEqualWithAccuracy(sut.dateFromString(nowToJSON)!.timeIntervalSince1970, now.timeIntervalSince1970, accuracy: 0.001)
}

You can see this code live here.

Leave the codebase better than you found it.

Want more of these posts?

Subscribe to receive new posts in your inbox.