WHY Got the wrong Timezone?

Hey folks!
Has anybody an idea why I get the wrong time here:

let aSimpleDate = "07/04/2018"
let formattierer = DateFormatter()
formattierer.dateFormat = "dd/MM/yyyy"
formattierer.timeZone = TimeZone.current
let cg = formattierer.date(from: aSimpleDate)
print ("Datum lautet:",cg!)

...gives the output: 2018-04-06 22:00:00 +0000

I also tried: NSTimeZone(name: "de_DE") as TimeZone?
to get the timezone Germany, but it doesn't work :frowning:

The debugDescription for Date always uses UTC. This is because, internally, Date is just an offset from a specific moment in time, and it has no knowledge of time zones. You have to use a DateFormatter if you want time zone-sensitive printing.

but I use:
formatierer = DateFormatter()
formatierer.timeZone = TimeZone.current

what is wrong there?

You used the formatter to create the date instance, but not to print it. The date printed is "2018-04-07 00:00 +0200", which is what you intended. It's just that it prints using UTC, which is 2 hours behind your local time zone, and that is 22:00 on the previous day.

To get a string form using your specified time zone, use formattierer.string(from: cg).

(I am writing from memory, so the exact method signature may be different.)

I also tried it, gave me the output:

Cannot convert value of type 'String' to expected argument type 'Date'
then I tried:
let cc = formattierer.date(from: aSimpleDate)
let cg = String(cc!) and got the output:
Cannot invoke initializer for type 'String' with an argument list of type '(Date)'

Try the following in a playground

let ds = "2018-04-03"
let df = DateFormatter()

df.dateFormat = "YYYY-MM-dd"
let date = df.date(from: ds)!

print(df.string(from: date))

thanks a lot!!!

Uh, that's very wrong. Please always use yyyy-MM-dd.
The lowercase y is correct for your usecase.

See http://www.unicode.org/reports/tr35/tr35-dates.html#dfst-year

I realized that after I posted, but it didn't affect the point of the example, so I left it.

1 Like

Wasn't meant as criticism of the answer, but rather as strong warning.
There should not be any tutorials, answers, etc… out there that show this easy to make mistake because it is simply the wrong answer.

Sorry for my (too) strong wording.

PS: I have made this error many times because I copied stuff from Stackoverflow ;)

but YYYY gives me the same output as yyyy

It is useful to follow the links in answers :slight_smile:

Then you'll find out that while most of the year it is indeed the same, it is not when you are close to the beginning and end of the year.

The capital Y is NOT correct.

Here's another explanation with samples


NSTimeZone(name: "de_DE") is wrong.
It should fix to vaild time zone name like as this list.

let tz0 = NSTimeZone(name: "de_DE") as TimeZone?
let tz1 = NSTimeZone(name: "Europe/Berlin") as! TimeZone
let tz2 = TimeZone(identifier: "Europe/Berlin")!
1 Like

Uh, that's very wrong. Please always use yyyy-MM-dd.

Indeed. Also, if you’re going to use a fixed date format string, also fix the locale to en_US_POSIX. There’s a world of things that can go wrong otherwise. My response on your other thread explains the three valid ways to use DateFormatter.

Share and Enjoy

Quinn “The Eskimo!” @ DTS @ Apple

Terms of Service

Privacy Policy

Cookie Policy