Skip to content

Instantly share code, notes, and snippets.

@dinizgb
Last active June 30, 2023 16:15
Show Gist options
  • Save dinizgb/e32b9da94432128eeed12c8e281ab31d to your computer and use it in GitHub Desktop.
Save dinizgb/e32b9da94432128eeed12c8e281ab31d to your computer and use it in GitHub Desktop.
Javascript function to get last dates within a determined period.
/**
* Function to get last dates within a determined period
* @param {Number} period - With the period
* @returns {Array} With an Array with dates in ISO format (YYYY-MM-DDTHH:MM:SS.sssZ) as string
*/
export const getLastDatesWithinPeriod = (period: number): Array<string> => {
const today = new Date();
const days = [];
for (let i = 0; i < period; i++) {
const day = new Date(today);
day.setDate(day.getDate() - i);
days.push(day.toISOString());
}
return days;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment