shubhiSingh
shubhiSingh

Reputation: 39

How to calculate time(in Hours) from selected time(Hours) to given duration

For eg, if I have selected Time: 3pm and the number of hours: 5 hours, then get (8pm) as answer"

Upvotes: 2

Views: 74

Answers (1)

Nishant Bhindi
Nishant Bhindi

Reputation: 2252

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())

Now you can get hour

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

print(hour)// This is your answer.

Upvotes: 2

Related Questions