Skip to content

Instantly share code, notes, and snippets.

@dgrammatiko
Created August 26, 2024 08:52
Show Gist options
  • Save dgrammatiko/5b2e18d8cd5e1109596199043adf3655 to your computer and use it in GitHub Desktop.
Save dgrammatiko/5b2e18d8cd5e1109596199043adf3655 to your computer and use it in GitHub Desktop.
/**
* @param {string} locale
* @param {string} type
* @returns {Array<String>}
*/
function getMonthsNames(locale = "en-GB", type = "short") {
const fmt = new Intl.DateTimeFormat(locale, { month: type });
const months = [];
for (let month = 0; month < 12; month++) {
months.push(fmt.format(new Date(Date.UTC(2000, month, 1, 0, 0, 0))));
}
return months;
}
console.log(getMonthsNames()); //['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
console.log(getMonthsNames("en-GB", "long")); //['January', 'February', 'March', 'April', 'May', 'June', 'July', 'August', 'September', 'October', 'November', 'December']
console.log(getMonthsNames("el-GR")); //['Ιαν', 'Φεβ', 'Μαρ', 'Απρ', 'Μαΐ', 'Ιουν', 'Ιουλ', 'Αυγ', 'Σεπ', 'Οκτ', 'Νοε', 'Δεκ']
console.log(getMonthsNames("el-GR", "long")); //['Ιανουαρίου', 'Φεβρουαρίου', 'Μαρτίου', 'Απριλίου', 'Μαΐου', 'Ιουνίου', 'Ιουλίου', 'Αυγούστου', 'Σεπτεμβρίου', 'Οκτωβρίου', 'Νοεμβρίου', 'Δεκεμβρίου']
/**
* @param {string} locale
* @param {string} type
* @param {0|1|2|3|4|5|6} firstDay
* @returns {Array<String>}
*/
function getDaysNames(locale = "en-GB", type = "short", firstDay = 0) {
const fmt = new Intl.DateTimeFormat(locale, { weekday: type });
const days = [];
for (let day = firstDay; day < firstDay + 7; day++) {
days.push(fmt.format(new Date(Date.UTC(2000, 12, day, 0, 0, 0))));
}
return days;
}
console.log(getDaysNames()); //['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat']
console.log(getDaysNames("en-GB", "long")); //['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']
console.log(getDaysNames("el-GR")); //['Κυρ', 'Δευ', 'Τρί', 'Τετ', 'Πέμ', 'Παρ', 'Σάβ']
console.log(getDaysNames("el-GR", "long")); //['Κυριακή', 'Δευτέρα', 'Τρίτη', 'Τετάρτη', 'Πέμπτη', 'Παρασκευή', 'Σάββατο']
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment