I'm working to create a function that takes an input duration and an "end" time, and calculates a "start" time based on that criteria.
Example: if the "end" time is 5:00pm and the "duration" is 30 minutes, then the "start" time returned should be 4:30pm.
I'm getting lost in translating between Int/Date/String, so I may need to re-think my entire approach. So far I'm working with the following, which is just a take on this Stack Overflow post:
var userInputEventEndTime: String
var userInputEventDuration: Int
var userInputEventNeedsToStartTime: String
//Manipulate this function with the input parameters
func makeDate(hr: Int, min: Int, sec: Int) -> Date {
var calendar = Calendar(identifier: .gregorian)
let components = DateComponents(hour: hr, minute: min, second: sec)
let hour = calendar.component(.hour, from: calendar.date(from: components)!)
let minutes = calendar.component(.minute, from: calendar.date(from: components)!)
let seconds = calendar.component(.second, from: calendar.date(from: components)!)
return calendar.date(from: components)!
}
Should I be converting the input strings to DateComponents and then doing the math? If so, I'm not sure how to account for minutes converting to hours. Or just go a different direction altogether? Thanks!