From Apple's documentation:
A Date is independent of a particular calendar or time zone. To
represent a Date to a user, you must interpret it in the context of a
Calendar.
You need the help of Calendar
:
let calendar = Calendar(identifier: .gregorian)
let currentDate = Date()
print(calendar.dateComponents(in: calendar.timeZone, from: currentDate))
the output will be:
calendar: gregorian (fixed) timeZone: Europe/Bratislava (current) era: 1 year: 2019
month: 11 day: 19 hour: 15 minute: 47 second: 14 nanosecond: 285109996 weekday: 3 weekdayOrdinal: 3 quarter: 0 weekOfMonth: 4 weekOfYear: 47 yearForWeekOfYear: 2019 isLeapMonth: false
So you could then access your calendar
's components like hour
, minute
etc.
UPDATE:
@Camile answered in comments to their question using DateFormatter
:
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy-MM-dd HH:mm:ss:Z"
dateFormatter.timeZone = calendar.timeZone
dateFormatter.string(from:currentDate)
will give you
2019-11-19 16:38:03:+0100