I would like to obtain Japan time from the Date class.

Asked 2 years ago, Updated 2 years ago, 77 views

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.

swift ios

2022-09-30 21:47

1 Answers

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.


2022-09-30 21:47

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.