Ecco una funzione che farà il trucco (se non utilizzano momento, ma solo la vaniglia JavaScript):
var getDaysArray = function(year, month) {
var names = [ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ];
var date = new Date(year, month - 1, 1);
var result = [];
while (date.getMonth() == month - 1) {
result.push(date.getDate() + "-" + names[date.getDay()]);
date.setDate(date.getDate() + 1);
}
return result;
}
Ad esempio:
js> getDaysArray(2012,2)
["1-wed", "2-thu", "3-fri", "4-sat", "5-sun", "6-mon", "7-tue",
"8-wed", "9-thu", "10-fri", "11-sat", "12-sun", "13-mon", "14-tue",
"15-wed", "16-thu", "17-fri", "18-sat", "19-sun", "20-mon", "21-tue",
"22-wed", "23-thu", "24-fri", "25-sat", "26-sun", "27-mon", "28-tue",
"29-wed"]
ES2015 + versione:
const getDaysArray = (year, month) => {
const names = Object.freeze(
[ 'sun', 'mon', 'tue', 'wed', 'thu', 'fri', 'sat' ]);
const date = new Date(year, month - 1, 1);
const result = [];
while (date.getMonth() == month - 1) {
result.push(`${date.getDate()}-${names[date.getDay()]}`);
date.setDate(date.getDate() + 1);
}
return result;
}
Suppongo che debba essere "04-sat" anziché "04-say" ", seguito da" 05-sun "," 06-mon "," 07-tue "," 08- wed "' e così via? Perchè ti serve? –
Sì, diciamo era un errore di battitura. Ho bisogno che questo venga visualizzato in un menu a discesa in modo che l'utente possa scegliere un anno e un mese, quindi un giorno all'interno della combinazione anno-mese. Ma piuttosto che mostrare 1,2,3 ... per i giorni vorrei anche mostrare il nome del giorno 1-mon, 2-tues, 3-wed ... – ecorvo
Vedere la mia risposta qui sotto. Allo scopo di far scegliere una data a qualcuno, tuttavia, ti consiglio di utilizzare uno dei tanti selettori di date del calendario JavaScript disponibili. –