I am using following code to parse the time stamp value. But when I convert it to date it is giving correct value (03-08-2021) but then I tried to convert this date to string to fulfil my requirements.
But that string resulting a wrong value 03-07-2021. Following is the code I am using.
let epocTime = TimeInterval(1615161600000/1000)
let date = Date(timeIntervalSince1970: epocTime)
print(date)
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd"
dateFormatter.timeZone = .current
dateFormatter.locale = .current
let dateString = dateFormatter.string(from: date)
print(dateString)
You are mixing a Date
- which is a point in time - with a formatted Date string, which is something you can see on a watch or on a calendar.
Date
is always referencing UTC (f.k.a. Greenwich Mean Time), so the output of print(date)
will not print a calendar date but the UTC date and time.
If you convert that date to a "calendar entry" (by specifing a time zone etc.), you then will get the value that is displayed on your local watch, when in UTC its midnight on the 8th of March, 2021.
If you are living west of Greenwich, this will be the day before, because you are still on the 7th of March, waiting for the new day to arrive.