Last active
August 30, 2023 17:26
-
-
Save yairEO/39822cc583d833457f6e to your computer and use it in GitHub Desktop.
auto-generates month names in any locale
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Much simpler:
let months = Array.from (Array(12), (v, i) => new Date (0,i).toLocaleString ({},{month:'short'}))
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
//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
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???!