I'm looking for a way to display the current date in a label with Swift 3. Currently I'm using DateFormatter.dateFormat but the only output I get is
dd.MM.yyyy
Here is the line of code:
lbl_Date.text = DateFormatter.dateFormat(fromTemplate: "MMddyyyy", options: 0, locale: NSLocale(localeIdentifier: "de-AT") as Locale)
And my expected output for today should be:
11.12.2016
I know that I can get the time with the dateTimeComponents but I haven't figured out how I can get only the date (day, month and year) in my label:
// get the current date and time
let currentDateTime = Date()
// get the user's calendar
let userCalendar = Calendar.current
// choose which date and time components are needed
let requestedComponents: Set<Calendar.Component> = [
.year,
.month,
.day,
.hour,
.minute,
.second
]
// get the components
let dateTimeComponents = userCalendar.dateComponents(requestedComponents, from: currentDateTime)
What is there the cleanest way to display the current date in my label?