How can I get the last Friday of each month for the next N months in Rust? I am able to get every Friday for the next N weeks but not able to find out how to determine if it is the last Friday of a month.
Currently I have:
use chrono::{Date, Datelike, DateTime, Duration, NaiveDate, NaiveDateTime, Utc, Weekday};
...
while index < 52 {
// Works to get friday at midnight
let new_date = NaiveDate::from_isoywd_opt(
now.iso_week().year(),
now.iso_week().week(),
Weekday::Fri
).unwrap();
let naive_datetime: NaiveDateTime = new_date.and_hms(0, 0, 0);
log::debug!("{:#?}", naive_datetime);
now = now + Duration::weeks(1);
index += 1;
}
But strangely I cannot find an easy way to determine the month cadence for this. I must be missing something obvious.
I was able to get it to work with the hint from @Jmb with this code block.