2009-06-22 15 views

risposta

42
var firstDay = new Date("2009/06/25"); 
var nextWeek = new Date(firstDay.getTime() + 7 * 24 * 60 * 60 * 1000); 

Potete anche guardare DateJS se si API come "fluente".

+1

Non funziona per settimana con le modifiche all'ora legale, però :( – etienne

+1

@etienne, hai provato datejs? Sono solo curioso, non l'ho ancora testato con l'ora legale. –

+0

Non ancora. Ma attualmente sto combattendo con DST - da quando abbiamo cambiato questo week-end :( – etienne

9
Date.prototype.addDays = function (d) { 
    if (d) { 
     var t = this.getTime(); 
     t = t + (d * 86400000); 
     this.setTime(t); 
    } 
}; 

this_week.addDays(7); 
+0

mi piace ...! +1 – Cerebrus

+1

Vorrei lasciare solo il prototipo della data. – glmxndr

+1

Perché lasciare il prototipo della data da solo? – razzed

28
function nextweek(){ 
    var today = new Date(); 
    var nextweek = new Date(today.getFullYear(), today.getMonth(), today.getDate()+7); 
    return nextweek; 
} 
+0

Nice - La funzione è autodocumentante e funziona con casi limite come il bisestile – l0b0

+0

Bel lavoro, con un leggero adattamento puoi trovare un giorno della settimana (es: prossimo giovedì). ad l'ultimo argomento, today.getDate() - today.getDay() + 7 + 4 (che funziona per giovedì) – Lathan

+1

Questo potrebbe generare alcune date non valide tuttavia, che può o potrebbe non funzionare, a seconda di quanto intelligenti siano le funzioni Date() dei motori JS target. Ad esempio una settimana dopo il 30 agosto 2013 sarà il 37 agosto 2013, che non è valido. – Shayne

8

function dateObject.getNextWeekDay restituisce il giorno della settimana successiva alla data dell'oggetto stesso.

Date.prototype.getNextWeekDay = function(d) { 
 
    if (d) { 
 
    var next = this; 
 
    next.setDate(this.getDate() - this.getDay() + 7 + d); 
 
    return next; 
 
    } 
 
} 
 

 
var now = new Date(); 
 
var nextMonday = now.getNextWeekDay(1); // 0 = Sunday, 1 = Monday, ... 
 
var secondNextMonday = new Date(nextMonday).getNextWeekDay(1); 
 
console.log('Next Monday : ' + nextMonday); 
 
console.log('Second Next Monday : ' + secondNextMonday);

+0

merita di essere la migliore risposta –

+0

questo è distruttivo, aggiorna l'argomento. http://stackoverflow.com/questions/1090815/how-to-clone-a-date-object-in-javascript –

+1

E inquina il prototipo Date - vedi http://programmers.stackexchange.com/questions/104320/why -is-estendere-il-dom-built-in-object-prototypes-a-bad-idea – tuomassalo

1

funzione che restituisce prossimo weekdaay:

function getNextWeekDay (startDate, dayOfWeek){ 
    var dayOffset = dayOfWeek > startDate.getDay() 
     ? dayOfWeek - startDate.getDay() 
     : dayOfWeek - startDate.getDay() + 7; 

    startDate.setDate(startDate.getDate() + dayOffset); 

    return startDate; 
} 

var now = new Date(); 

var nextMonday = getNextWeekDay(new Date(),1); // 0 = Sunday, 1 = Monday, ... 
var nextSaturday = getNextWeekDay(new Date(),6); 
var nextSunday = getNextWeekDay(new Date(),0); 
var secondNextMonday = getNextWeekDay(new Date(now.getTime() + (7 *24 * 60 * 60 * 1000)),1); 
alert(nextMonday+ '- '+nextSaturday+ '- '+nextSunday+ '- '+secondNextMonday); 
Problemi correlati