Open herculewyy opened 1 year ago
Starting from 1.31, the frequency is every month, then I want to get the last day of February (2.28 or 2.29)
Feb 29, 2023 is not a real date
Maybe something along the lines of the 30th of every month or last day.
Probably done with a ruleset
the following code works for me
func printTime(ts []time.Time) {
for _, t := range ts {
fmt.Println(t)
}
}
func main() {
loc, _ := time.LoadLocation("Local")
curr := time.Date(2024, 1, 28, 12, 45, 23, 0, loc)
r, _ := rrule.NewRRule(rrule.ROption{
Freq: rrule.MONTHLY,
Interval: 1,
Bymonthday: []int{-1},
Dtstart: curr,
Until: curr.AddDate(0, 5, 0),
})
printTime(r.All())
}
output:
2024-01-31 12:45:23 +0800 CST
2024-02-29 12:45:23 +0800 CST
2024-03-31 12:45:23 +0800 CST
2024-04-30 12:45:23 +0800 CST
2024-05-31 12:45:23 +0800 CST
result: 2023-01-30 12:45:23 2023-03-30 12:45:23 2023-04-30 12:45:23 2023-05-30 12:45:23 2023-06-30 12:45:23
2023-02-28 12:45:23 How to achieve?
thank you