Hello
I wanted to know why when I print(Date()) the date is printed but two hours before?
Example
If I print(Date()) and the actual date is 2021-09-10 22:33:41 +0000, it prints 2021-09-10 20:33:41 +0000 instead of 2021-09-10 22:33:41 +0000.
Why does this happen?
Thank You!
This is because the date is stored in Swift as UTC time, i.e. as at longitude 0.0 - which used to be called Greenwich Mean Time. It doesn't take into account your timezone, nor any Summertime adjustment. When you display the time to the user in your code, via UIKit or SwiftUI, you use a DateFormatter (in UIKit) or Text(date, style: .date) in SwiftUI and the system then recognises and applies your device's timezone (including summertime status) and language. SwiftUI has only limited date formatting capabilities, so I normally use my own formatting function e.g.in SwiftUI Text(myAppAPI.dateToString(date)) where myApAPI is a collection of useful functions I have in a class (myAppAPI) and dateToString is a function that applies the DateFormatter() function, with my applied options, of Swift.
When you use print(date) no formatting is applied, therefore you get the UTC time.