Swift NSDateFormatter return a strange year 0005 instead 2023

94 Views Asked by At

I am using very simple date format but some devices returning a wrong year. This only happened on 1 device in Japan so I could not reproduce it.

let timeStamp = Date().timeIntervalSince1970

let date = Date(timeIntervalSince1970: timeStamp)
let dateFormatter = DateFormatter()
dateFormatter.locale = NSLocale.current
dateFormatter.dateFormat = format
let result = dateFormatter.string(from: date)

print(result)

The result is 0005-10-13

Anh help!

1

There are 1 best solutions below

1
Binh Ho On

Ok, I found the issue. If device set calendar to Japan this issue will happen. To fix it just add like below.

dateFormatter.calendar = Calendar(identifier: .gregorian)

Thank to @Martheen