Reputation: 3078
I am using this code to convert the NSDate
to my local timezone, the correct hour and day is shown but the AM/PM part is always incorrect.
let date1 = "2015-05-14 19:00:00 +0000"
let date2 = "2015-06-12 04:50:00 +0000"
var dateFormatter = NSDateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss zzzz"
let date = dateFormatter.dateFromString(date1)
let timeZone = NSTimeZone.localTimeZone()
let seconds: NSInteger = timeZone.secondsFromGMTForDate(date!)
var d = NSDate(timeInterval: NSTimeInterval(seconds), sinceDate: date!)
date1
comes back as May 14, 2015, 7:AM
date2
comes back as Jun 11, 2015, 4:50PM
Both of these should have opposite AM/PM values, can anyone tell me what I am doing wrong?
Upvotes: 0
Views: 165
Reputation: 539765
(From my above comment) NSDate
does not have a time zone. date1/date2 are strings specifying a time in GMT. If you want to print date
in your local time zone then use a date formatter to convert it to a string.
If your intention is to interpret date1/date2 as if they were in your local time zone instead of GMT then your correction goes into the wrong direction (replace seconds
by -seconds
).
Your results are not with opposite AM/PM values but simply 12 hours earlier than you expected them to be (which makes sense because Mountain time is GMT-06).
Upvotes: 1