Get current date in Swift 3?

yucel picture yucel · Sep 15, 2016 · Viewed 196.3k times · Source

How can I set label.text current date in Swift 3?

I want to print just today to the screen. I did not find how to do that.

In c# is very simple:

var date = DateTime.Now

I need to write 15.09.2016 in swift 3. thanks

Answer

Eric Aya picture Eric Aya · Sep 15, 2016

You say in a comment you want to get "15.09.2016".

For this, use Date and DateFormatter:

let date = Date()
let formatter = DateFormatter()

Give the format you want to the formatter:

formatter.dateFormat = "dd.MM.yyyy"

Get the result string:

let result = formatter.string(from: date)

Set your label:

label.text = result

Result:

15.09.2016