I am building an iOS app in Swift 2. I need to take a date and change the time for that date. How can I do that in the most simple way?
2016-12-10 12:00:00 to 2016-12-10 16:00:00
The time will come from a string but the date from an NSDate.
I am building an iOS app in Swift 2. I need to take a date and change the time for that date. How can I do that in the most simple way?
2016-12-10 12:00:00 to 2016-12-10 16:00:00
The time will come from a string but the date from an NSDate.
Simplest way to get 16:00 local time:
let d1 = Date()
let d2 = Calendar.current.date(bySettingHour: 16, minute: 0, second: 0, of: d1)!
Note that if you call print(d2)
, it will always print in GMT so the hour may not show up as 16:00, but it's 16:00 in your local timezone.