I have an array of dates that I want to round to various frequencies - weeks, months, quarters, and years. The equivalent of rounding numbers but for dates.
Here is a sample array:
const dates = [
"1/4/2020",
"2/5/2021",
"8/15/2021",
];
Rounding by month would yield:
[
"1/31/2020",
"2/28/2021",
"8/31/2021",
];
Nearest year:
[
"12/31/2020",
"12/31/2021",
"12/31/2021",
];
And so on. Is there a Javascript library that allows for such rounding? If not, is there a simple function that would cover the above frequencies? I can write a custom function for each use case but that'll likely be inefficient.