Strange behaviour in Xcode playground

Hi Everyone,

I am a new to swift and Xcode and thought I would follow Chris’ excellent youTube course to develop an App. In lesson 6 introduction to swift, I started to have a play (solid programming background, C, SmallTalk etc… although many years ago). I assigned a date to a variable and printed it. The date assigned and printed had a different time. Can anyone help?

I’m assuming you are in a time zone that is 1 hour ahead of GMT. The Date object is stored as the number of elapsed seconds since January 1, 2001 at 00:00:00 UTC. To get a localized date, you need to use a DateFormatter.

Add this to your playground and see for yourself:

print(DateFormatter.localizedString(from: newDT, dateStyle: .full, timeStyle: .full))

Hi Roosterboy,

Spot on that worked - thank you so much. However I am in GMT any idea why the print(nowDT) was an hour out. I know I have a solution and I will use it but just interested.


British Summer Time? Just a guess.