Skip to content

Instantly share code, notes, and snippets.

@yairEO
Last active August 30, 2023 17:26
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save yairEO/39822cc583d833457f6e to your computer and use it in GitHub Desktop.
Save yairEO/39822cc583d833457f6e to your computer and use it in GitHub Desktop.
auto-generates month names in any locale
var months = [];
for( var i = 0; i < 12; i++ ){
months.push( new Date(0,i).toLocaleString({},{month:'short'}) );
// you can also pass a local like : "en-US" instead of an empty object `{}`.
// an empty object triggers the system's auto-detection
}
console.log(months);
@remoteportal
Copy link

I ran your code and the weirdest thing happened: I got:

January,February,March,March,April,May,June,July,August,September,October,December

Double March???!

@SkepticaLee
Copy link

Much simpler:

let months = Array.from (Array(12), (v, i) => new Date (0,i).toLocaleString ({},{month:'short'}))

@Fanna1119
Copy link

    var monthsfunc = (monthnum) =>
      new Date(2019, monthnum).toLocaleString("default", { month: "long" });

    const monthrange = [...Array(12).keys()].map(monthsfunc);
    console.log(monthrange);

another way but @SkepticaLee had a much easier version :D

@jankyupeblik
Copy link

//the number 12 is irrelevant
var date = new Date();
var year = date.getFullYear();
var months = [];
var month = 0;
do {
    months.push(new Date(year,month).toLocaleString('default',{month:'long'}));
    month = new Date(year,month+1).getMonth();
} while (month > 0)
console.log(months);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment