Skip to content

Instantly share code, notes, and snippets.

@ik5
Last active June 23, 2021 17:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ik5/5831d2246cfec2a0c27b82fd9c7fa338 to your computer and use it in GitHub Desktop.
Save ik5/5831d2246cfec2a0c27b82fd9c7fa338 to your computer and use it in GitHub Desktop.
Example on how to look for the date based on a day of the week
package main
import (
"fmt"
"time"
)
func main() {
t, _ := time.Parse("2006-01-02 15:04", "2021-05-17 10:55")
today := t.Weekday()
now := time.Now()
day := time.Sunday
var next time.Time
if day == today {
hour := now.Hour()
reqHour := t.Hour()
if hour >= reqHour { // if hour is now, or equal provide next week
next = t.AddDate(0, 0, 7)
} else if reqHour < hour { // calculate tomarrow instead of today
next = t.AddDate(0, 0, 1)
} else { // today !!!
next = t
}
} else if day > today {
next = t.AddDate(0, 0, int(day-today))
} else {
next = t.AddDate(0, 0, int(day+(7 - today)))
}
fmt.Println(t, today, day, next, next.Weekday())
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment