2012-07-04 13 views
5

come posso ottenere l'intero mese in nomi giorno di un mese/anno? come:JavaScript: ottieni un array di nomi giorno della data data (mese/anno)

var year = "2000"; 
var month = "7" 

... some code here that makes an array with names of days of the given month... 

e l'uscita sembra qualcosa di simile:

Array ("1. Sunday", "2. Monday", "3. Tuesday", ... "29. Thursday", "30. Friday", "31. Saturday"); 

Con i migliori saluti, Chris

+0

Questa non è una domanda "JavaScript" in quanto è una domanda "che data API voglio utilizzare". La parte JavaScript è facile come torta una volta trovata un'API che determinerà quali giorni della settimana sono caduti in un particolare mese di un determinato anno. – natlee75

+0

Quale output ti aspetti per questa data. Sabato? –

+0

Scratch che ... non dovrebbe essere troppo difficile per niente ... non pensare chiaramente così tardi :-) – natlee75

risposta

9

Questo dovrebbe fare ciò che hai chiesto.

function getDaysArray(year, month) { 
    var numDaysInMonth, daysInWeek, daysIndex, index, i, l, daysArray; 

    numDaysInMonth = [31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31]; 
    daysInWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; 
    daysIndex = { 'Sun': 0, 'Mon': 1, 'Tue': 2, 'Wed': 3, 'Thu': 4, 'Fri': 5, 'Sat': 6 }; 
    index = daysIndex[(new Date(year, month - 1, 1)).toString().split(' ')[0]]; 
    daysArray = []; 

    for (i = 0, l = numDaysInMonth[month - 1]; i < l; i++) { 
     daysArray.push((i + 1) + '. ' + daysInWeek[index++]); 
     if (index == 7) index = 0; 
    } 

    return daysArray; 
} 
+0

Se vuoi che aggiunga commenti per spiegare cosa sto facendo ad ogni passaggio, fammelo sapere e modifico il post. – natlee75

+0

Grazie mille! Questo e 'esattamente quello che stavo cercando. Grazie ancora! – Chris

2

Prova questa

<!DOCTYPE html> 
<html> 
<body> 

<p id="demo">Click the button to display todays day of the week.</p> 

<button onclick="myFunction()">Try it</button> 

<script type="text/javascript"> 
function daysInMonth(month,year) { 
    return new Date(year, month, 0).getDate(); 
} 

function myFunction() 
{var b=[],weekday = ['Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday'],month=7,year=2000; 
for(var i=1,l=daysInMonth(month,year);i<l;i++){ 
var d = new Date(year,month-1,i); 
b.push(i+"."+weekday[d.getDay()]); 
} 
console.log(b);}//b is the desired array 
</script> 

</body> 
</html> 
+0

Va notato che nel tuo 'myFunction()', 'month' è 1-based, non zero- basato; questo è l'unico modo in cui la tua implementazione di 'daysInMonth' funzionerà correttamente. Ciò potrebbe causare confusione poiché l'oggetto 'Date' di javascript utilizza un valore di mese a base zero – jackwanders

+0

No, il mese è a base zero. Vedi https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/ – scottheckel

+0

Inoltre, l'utilizzo di 'new Array' per il tuo array' weekday' è inefficiente, è meglio fare 'weekday = ['Sunday ', 'Lunedi', 'Martedì', 'Mercoledì', 'Giovedi', 'Venerdì', 'Sabato'] '. Inoltre, non è necessario ricreare l'array in ogni iterazione del ciclo 'for'. – jackwanders

0

È possibile fare la logica da soli abbastanza facilmente utilizzando l'oggetto JavaScript Date standard. Ecco un esempio di ottenere una data da inserire nel tuo array. Segui l'intero mese chiamando il metodo. (jsFiddle)

var daysOfTheWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday']; 

function getDisplay(month, day, year) { 
    var d = new Date(year, month-1, day); 
    return day + '. ' + daysOfTheWeek[d.getDay()]; 
} 

array_push($dates, getDisplay(7, 4, 2012)); 
+0

penso che ci dovrebbe essere 'mese + 1' – diEcho

+0

No, il mese è a base zero. Gennaio è zero. Ecco perché l'ho sostituito in quella funzione per corrispondere con le sue variabili. https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Date/ – scottheckel

+0

questo è il motivo per cui se l'utente inserisce luglio ('7') allora lo script java dovrebbe interpretarlo come' 6' – diEcho

0

Provare questo e vedere il risultato in console.

<script> 
var weekday = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 
       'Thursday', 'Friday', 'Saturday']; 
var month = '7'; 
var year = '2012'; 
realMonth = parseInt(month,10)-1; 
var monthObj = {}; 
for(var i=1;i<30;i++) 
{ 
    var d = new Date(year,realMonth,i);    
    monthObj[i] = weekday[d.getDay()]; 
} 
console.log(monthObj); 
</script> 
Problemi correlati