I set Japan time for the Date class as the code below.
let dateFormatter=DateFormatter()
dateFormatter.timeZone=TimeZone(identifier: "Asia/Tokyo")
dateFormatter.locale=Locale(identifier: "ja_JP")
dateFormatter.dateFormat="yyyy-MM-dd HH:mm:ss"
let date=dateFormatter.date(from: "2020-03-17 11:00:00")
print("test="+date!.description)
The expected value of the print statement is "2020-03-17 11:00 + 0900"
However, the actual output of the GMT time is as follows.
"test=2020-03-1702:00:00+0000"
What should I do to get Japan time?
Thank you in advance.
let date=dateFormatter.date(from: "2020-03-17 11:00:00")
print("test="+date!.description)
let date=dateFormatter.date(from: "2020-03-17 11:00:00")
let dateString = dateFormatter.string (from:date!)
print("test="+dateString)
By doing so, I was able to get the string of Japan time.
© 2024 OneMinuteCode. All rights reserved.