2009-03-03 18 views
22

dati un oggetto data, come ottenere il suo primo giorno del mese precedente in javascriptTrova il primo giorno del mese precedente in javascript

+0

un titolo più descrittivo potrebbe essere bello ... – helloandre

+0

ho aggiornato il titolo – Prestaul

+0

http://stackoverflow.com/questions/594054/date-problem-in-java assomiglia alla tua domanda precedente era per Java. quindi non esattamente un duplicato :) –

risposta

32
function firstDayInPreviousMonth(yourDate) { 
    var d = new Date(yourDate); 
    d.setDate(1); 
    d.setMonth(d.getMonth() - 1); 
    return d; 
} 

EDIT: Va bene ... Ho sicuramente imparato qualcosa qui. Credo che questa sia la soluzione più semplice che copre tutti i casi (e sì, funziona per gennaio):

function firstDayInPreviousMonth(yourDate) { 
    return new Date(yourDate.getFullYear(), yourDate.getMonth() - 1, 1); 
} 
+0

Nota che questo FAIL per qualcosa di simile: firstDayInPreviousMonth (new Date ("Mar 31 2009")). Vedere i commenti su: http://stackoverflow.com/questions/499838/javascript-date-next-month – Shog9

+0

Non fallirebbe se impostasse DateDate (1) prima di eseguire il setMonth() –

+0

Sì, funziona bene. – Shog9

12

Il seguente dovrebbe funzionare:

now = new Date(); 
if (now.getMonth() == 0) { 
    current = new Date(now.getFullYear() - 1, 11, 1); 
} else { 
    current = new Date(now.getFullYear(), now.getMonth() - 1, 1); 
} 

tenendo presente che mesi sono zero basato in modo di dicembre è piuttosto che 12.

Ma, come altri hanno fatto notare, gli impacchi mese, anche come parte del costruttore atomica, in modo che il seguente è anche possibile:

0.123.
now = new Date(); 
firstDayPrevMonth = new Date(now.getFullYear(), now.getMonth() - 1, 1); 
+1

Mi piace questo approccio, ma la tua soluzione in realtà non è corretta. Quando getMonth() == 1, il mese è febbraio.Si desidera verificare getMonth() == 0 e anche questo non è necessario in quanto il costruttore Date accetterà valori negativi e funzionerà correttamente. – Prestaul

+0

Stai trascurando il fatto che setMonth avvolgerà i valori. La risposta di Prestaul funziona bene per gennaio. – overslacked

+0

Sì, ma non per tornare a un mese in cui il numero di giorni è inferiore, sfortunatamente. È meglio costruire la nuova data atomicamente piuttosto che frammentare. – paxdiablo

2

Mi piace questa soluzione. Potrebbe non essere il più breve, ma mette in evidenza alcune funzioni del metodo setDate() su Data() oggetti che non tutti avranno familiarità con:

function firstDayPreviousMonth(originalDate) { 
    var d = new Date(originalDate); 
    d.setDate(0); // set to last day of previous month 
    d.setDate(1); // set to the first day of that month 
    return d; 
} 

si avvale del fatto che .setDate (0) cambierà la data in modo che punti all'ultimo giorno del mese precedente, mentre .setDate (1) lo cambierà (ulteriormente) per puntare al primo giorno di quel mese. Permette alle librerie di base di Java di fare il lavoro pesante.

È possibile vedere uno working Plunk here.

0

Sarà utile per ottenere il mese precedente prima e l'ultima data.

function getLastMonth(){ 
     var now = new Date(); 
     var lastday = new Date(now.getFullYear(), now.getMonth(), 0); 
     var firstday = new Date(lastday.getFullYear(), lastday.getMonth(), 1); 
     var lastMonth = firstday.getDate()+'/'+(firstday.getMonth()+1)+'/'+firstday.getFullYear()+' - '+lastday.getDate()+'/'+(firstday.getMonth()+1)+'/'+lastday.getFullYear(); 
     return lastMonth; 
} 
Problemi correlati