I am still fairly new to Go and I'm refactoring a bunch of my existing code. In a lot of places, I have used 86400 * { days }
for setting things like MaxAge, etc.
Is there a more intuitive way to set these? I have a lot more use cases now where I need to use time validations and things, but I am wondering how to do something like time in 2 standard weeks, or time in 6 standard months, etc.
Thanks
Use AddDate
twoWeeksFromNow := time.Now().AddDate(0, 0, 14)
oneMonthFromNow := time.Now().AddDate(0, 1, 0)
oneYearFromNow := time.Now().AddDate(1, 0, 0)
This is what the Duration type is for, along with its predefined constants.
Although be aware that it doesn't always handle days gracefully, due to timezone and daylight savings time changes, etc. If you're okay with that, do something like:
maxAge := days * 24 * time.Hour
To add this to the current time, you can use Add:
deadline := time.Now().Add(maxAge)