Skip to content

Instantly share code, notes, and snippets.

@hebertcisco
Created December 4, 2021 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hebertcisco/3964ff6dd02735b2d85ef7f3aac41ea3 to your computer and use it in GitHub Desktop.
Save hebertcisco/3964ff6dd02735b2d85ef7f3aac41ea3 to your computer and use it in GitHub Desktop.
Funtion to get the last months as string eg: 'jan'
export const getLastMonths = (many: number): string[] => {
const months = [];
const currentMonth = new Date().getMonth();
for (let i = 0; i < many; i++) {
months.push(
new Date(new Date().setMonth(currentMonth - i)).toLocaleString(
'default',
{ month: 'short' },
),
);
}
return months.reverse();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment