How to get local time zone date in swift 3
11,469
let date = Date().description(with: Locale.current)
print("date ---> \(date)")
Result: date ---> Tuesday, June 20, 2017 at 4:35:15 PM India Standard Time
I'm getting perfect system/local time.
You code is working,
let today=Date()
var calendar = Calendar.current
calendar.timeZone = .current
let components = calendar.dateComponents([.year, .month, .day, .hour, .minute], from: today)
let day = components.day
let hour = components.hour
let minute = components.minute
print("day = \(day)\nhour = \(hour)\nminute = \(minute)")
Result:
day = Optional(20)
hour = Optional(16)
minute = Optional(35)
Author by
user1960169
Updated on June 19, 2022Comments
-
user1960169 almost 2 years
I am getting year,month and day from a given date in this way.
let today=Date() var calendar = Calendar.current calendar.timeZone = .current let components = calendar.dateComponents([.year, .month, .day], from: today) let day=components.day
But I'm getting one day ahead from my current day. How can I solve this?