Last active
August 4, 2016 16:19
-
-
Save bradymholt/ff752ade04fe7ce7b39b698a987610b2 to your computer and use it in GitHub Desktop.
(JavaScript) Prints days of week and months of year for a specified list of locales
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 locales = [ | |
'en', // English | |
'es', // Spanish | |
'fr' // French | |
]; | |
function printDaysOfTheWeek(locale) { | |
var daysOfTheWeek = []; | |
var monday = new Date(); | |
monday.setDate(monday.getDate() - (monday.getDay() + 6) % 7); | |
for (var i = 0; i <= 6; i++){ | |
var currentDow = new Date(); | |
currentDow.setDate(monday.getDate() + i); | |
var dowName = currentDow.toLocaleString(locale, {weekday: 'long'}); | |
daysOfTheWeek.push(dowName); | |
} | |
console.log(daysOfTheWeek); | |
} | |
function printMonthsOfTheYear(locale) { | |
var monthsOfTheYear = []; | |
var january = new Date(new Date().getFullYear(), 0, 1);; | |
for (var i = 0; i <= 11; i++){ | |
var currentMonth = new Date(); | |
currentMonth.setMonth(january.getMonth() + i); | |
var monthName = currentMonth.toLocaleString(locale, {month: 'long'}); | |
monthsOfTheYear.push(monthName); | |
} | |
console.log(monthsOfTheYear); | |
} | |
for (l of locales) { | |
printDaysOfTheWeek(l); | |
printMonthsOfTheYear(l); | |
} | |
/* | |
> ["Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"] | |
> ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] | |
> ["lunes", "martes", "miércoles", "jueves", "viernes", "sábado", "domingo"] | |
> ["enero", "febrero", "marzo", "abril", "mayo", "junio", "julio", "agosto", "septiembre", "octubre", "noviembre", "diciembre"] | |
> ["lundi", "mardi", "mercredi", "jeudi", "vendredi", "samedi", "dimanche"] | |
> ["janvier", "février", "mars", "avril", "mai", "juin", "juillet", "août", "septembre", "octobre", "novembre", "décembre"] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment