在Swift 3中为明天设定时间

时间:2017-04-19 09:30:02

标签: swift swift3

我需要在特定时间为明天设置Date对象。我可以弄清楚如何将日期更改为明天而不是如何设置时间。这是我的功能:

klass

2 个答案:

答案 0 :(得分:2)

将小时和分钟传递给此功能:

func getTomorrowAt(hour: Int, minutes: Int) -> Date {
let today = Date()
let morrow = Calendar.current.date(byAdding: .day, value: 1, to: today)
return Calendar.current.date(bySettingHour: hour, minute: minutes, second: 0, of: morrow)

}

enter image description here

答案 1 :(得分:0)

let greg = Calendar(identifier: .gregorian)
let now = Date()
var components = greg.dateComponents([.year, .month, .day, .hour, .minute, .second], from: now)
components.hour = 10
components.minute = 35
components.second = 0

let date = greg.date(from: components)!