Reputation: 15512
In my playground i create a simple code example to convert time from my local zone to "America/Sao_Paulo"
.
let gmtDf: NSDateFormatter = NSDateFormatter()
gmtDf.timeZone = NSTimeZone.systemTimeZone();
gmtDf.dateFormat = "HH:mm:ss"
let gmtDate: NSDate = NSDate.new()
print(gmtDate)
let estDf: NSDateFormatter = NSDateFormatter()
estDf.timeZone = NSTimeZone(name: "America/Sao_Paulo")
estDf.dateFormat = "HH:mm:ss"
let estDate: NSDate = estDf.dateFromString(gmtDf.stringFromDate(gmtDate))!
print(estDate)
All code is working fine, but i have a question that is connected with code output.Why printed output show time 2 hours before my local time. And converted time is in 2000 year.
Upvotes: 0
Views: 116
Reputation: 2600
NSTimeZone.systemTimeZone
will return GMT time if it cannot determine the system time zone, I'm guessing that's what's happening here since you're using a Playground.
You're only using the hour, minutes and seconds to convert from date to string dateFormat = "HH:mm:ss"
so that String doesn't have any information about the year, that's why when you're using that to convert back to a NSDate it's showing 2000, since it doesn't have the proper info to know what year it is.
Upvotes: 1