Date in Swift printed is wrong

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!

Answered by AncientCoder in 687425022

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.

Accepted Answer

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.

And what if I wanted to write if Date() > Date() + 86400 for example, is it formatted?

Mm, comments don't format very well. Here it is again....

let currentHour = Calendar.current.component(.hour, from: Date())
if currentHour < 12 {print("now is morning")}

This gets the hour, as an Integer, from the current local time, i.e. the Calendar function converts the UTC time to the user's current timezone and summertime (or not) settings. 

I suggest that you experiment with Date formatting and with Calendar in a Playground.

let date = Calendar.current.dateComponents(in: .current, from: Date()).date! 
print(date)

The .date! at the end of the first line converts the date back to UTC.

To format the date in local time do this:

let df = DateFormatter()
df.dateStyle = .full
df.timeStyle = .full
print(df.string(from: Date()))

You have options of full, medium, long, short and none for both time and date. If this doesn't suit, you can set your own formatting

df.dateFormat = "yyyy-MM-dd HH:mm:ss.SS"

which right now would give 2021-09-11 10:14:23.33 in Sydney time. You adjust the formatting tokens to suit your own needs, e.g. MM-dd-yy HH:mm would give 09-11-21 10:14

Regards, Michaela

??? Other comments and my examples that we exchanged disappeared from the thread. Have you got things working now the way you want? I'm getting an error when trying to add a new comment to the thread below.

Date in Swift printed is wrong
 
 
Q