2014-08-31 5 views
13

Uso di Moment.js Mi piacerebbe ottenere tutti i giorni in un mese dell'anno specifico in un array. Ad esempio:Come ottenere l'elenco dei giorni in un mese con Moment.js

January-2014: 
[ 
"01-wed", 
"02-thr", 
"03-fri", 
"04-sat" 
] 

suggerimenti? Ho controllato i documenti di Moment.js ma non ho trovato nulla. L'armadio ho ottenuto è stato questo:

moment("2012-02", "YYYY-MM").daysInMonth() 

Ma questa restituire solo un int con i giorni totali per non determinato mese un array con ogni giorno.

+0

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? –

+0

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

+0

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. –

risposta

27

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; 
} 
+0

Grazie! Non c'è bisogno di un momento fintanto che fa il lavoro :) – ecorvo

+0

funziona perfettamente. grazie – anhnt

4

Alternativa con momentjs, lavorando per me

function getDaysArrayByMonth() { 
    var daysInMonth = moment().daysInMonth(); 
    var arrDays = []; 

    while(daysInMonth) { 
    var current = moment().date(daysInMonth); 
    arrDays.push(current); 
    daysInMonth--; 
    } 

    return arrDays; 
} 

E si può controllare

var schedule = getDaysArrayByMonth(); 
schedule.forEach(function(item) { 
    console.log(item.format("DD/MM")); 
}); 
7

Utilizzando anche lodash_.times:

var daysInMonth = []; 

var monthDate = moment().startOf('month'); // change to a date in the month of interest 

_.times(monthDate.daysInMonth(), function (n) { 
    daysInMonth.push(monthDate.format('D')); // your format 
    monthDate.add(1, 'day'); 
}); 
-3

Come di moment.js versione 2.1.0, quando si imposta una data come:

moment([2012, 0, 31]).month(1).format("YYYY-MM-DD"); // 2012-02-29 

Quindi devi solo analizzare la data e lo yo hai il numero di giorni in un mese