如何计算从所选时间(小时)到给定持续时间的时间(以小时为单位)

时间:2017-10-11 11:44:53

标签: swift

例如,如果我选择时间:下午3点和小时数:5小时,那么得到(晚上8点)作为答案"

1 个答案:

答案 0 :(得分:2)

let calendar = Calendar.current
let date = calendar.date(byAdding: .hour, value: 5, to: Date())
or
let date = calendar.date(byAdding: . minute, value: 330, to: Date())
  

现在你可以得到一小时

let comp = calendar.dateComponents([.hour, .minute], from: date)
let hour = comp.hour
let minute = comp.minute

print(hour)// This is your answer.