Yep. The array literal's type needs to be inferred, but that's still easier. There's only one operator +
, and it's in a position with only one applicable overload.
Calendrical calculations should be done using Calendar
and related classes. If your code is counting hours minutes and seconds it's almost certainly broken. Daylight savings time will mess everything up for you.
What's the best way to get a Date() value so many years, months, days, hours, minutes, seconds from now-this-moment? I used:
Date(timeInterval: secondsAhead, since: Date())
So I need to find secondsAhead
value.
Is there better way using Calendar/DateComponet etc?
Note that you can't simply add seconds to a time. You need to take into account the leap years/seconds, month structure, etc. I believe date(byAdding:to:wrappingComponents:) handles them for you.
I figured that's what you were doing. There are plenty of examples online but it will be something like this:
let calendar = Calendar(identifier: .gregorian)
var dateComponents = DateComponents()
dateComponents.day = 45
dateComponents.hour = 4
let newDate = calendar.date(byAdding: dateComponents, to: Date())
print("\(String(describing: newDate))")
Or to utilise the humungous DateComponents.init.
let calendar = Calendar(identifier: .gregorian)
let components = DateComponents(day: 45, hour: 4)
let newDate = calendar.date(byAdding: components, to: Date())
"\(newDate)"