Reputation: 46
We have an iOS app in production and we use some custom format for our date internal representation.
I stored a global date formatter like this:
static var InternalDateFormatter: NSDateFormatter = {
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyyMMdd HH':'mm"
return dateFormatter
}()
With a NSDate
instance of 2016-07-21T16:46:51+0000
the expected output is 20160721 16:46
, but in 2 phones the output is 20160712 4:41 p.m.
.
The device is an iPhone 5 with iOS 9.3.
I tried installing in a device, same model and same iOS version, and the output was right.
Could someone give me a glimpse into what's happening there?
Upvotes: 2
Views: 366
Reputation: 9612
You should set timezone
and locale
to your date formatter, try it like this:
static var InternalDateFormatter: NSDateFormatter = {
let dateFormatter: NSDateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyyMMdd HH':'mm"
dateFormatter.timeZone = NSTimeZone.localTimeZone()
dateFormatter.locale = NSLocale(localeIdentifier: "en_US_POSIX")
return dateFormatter
}()
Using en_US_POSIX
locale ID will always give you am-pm
US time standard.
But you can choose any locale ID - https://gist.github.com/jacobbubu/1836273
Upvotes: 2