Can I schedule a task to repeat only on weekdays?
seon1-kim opened this issue · comments
gocron.Every(1).Weekday(1).At("12:00").Do(func() {
SendMessage(api, *channelID, "2")
})
Above code does not work.
I want schedule jobs on weekday. except weekend.
Monday, Tuesday, ... It seems very inefficient to write every single day of the week code.
Function Weekday()
expects a time.Weekday
parameter, which is an integer representing the day of the week (Sunday = 0, Monday = 1, etc). The snippet you pasted here will run func every Monday (value 1). Maybe what you want is the function Day()
? You could treat inside you function to return in case of weekends.
I want to do repeated tasks only on weekdays.
The example code is shown below. I want to combine this into one.
lunch := "12:00"
gocron.Every(1).Monday().At(lunch).Do(func() {
sendSomething()
})
gocron.Every(1).Tuesday().At(lunch).Do(func() {
sendSomething()
})
gocron.Every(1).Wednesday().At(lunch).Do(func() {
sendSomething()
})
gocron.Every(1).Thursday().At(lunch).Do(func() {
sendSomething()
})
gocron.Every(1).Friday().At(lunch).Do(func() {
sendSomething()
})
Ok. I believe maybe you could do something along these lines?
gocron.Every(1).Day().At(lunch).Do(func() {
if time.Now().Weekday().String() == "Saturday" || time.Now().Weekday().String() == "Sunday" {
return
}
sendSomething()
})
It was not what I intended, but it was resolved. Thank you.
I’ll see what can be done in the future for your use case and see if we can implement it. PRs are welcome too :)