View Full Version : Date object and date arithmetics problem

08-24-2007, 12:42 PM

I have a function that is called when the page loads:

function loadStuff() {
today = new Date(); //today's date, e.g. Friday August 24, 2007

Then, I have a function that should change 'today' by one week up or down:

function changeWeek(direction) {
var oneMinute = 60 * 1000; //number of milliseconds in a minute
var oneHour = 60 * oneMinute;
var oneDay = 24 * oneHour;
var oneWeek = 7 * oneDay; //number of milliseconds in a week

if (direction == 'up') {
var newTodayInMS = today.getTime() + oneWeek;
} else {
var newTodayInMS = today.getTime() - oneWeek;

alert (today);

In the body of the page I have 2 buttons: one calls changeWeek('up') on click, the other calls changeWeek('down') on click.

Calling changeWeek('up') produces: Thu Sep 06 (i.e. 2 weeks in advance)
Calling changeWeek('down') produces: Thu Aug 23 (i.e. 1 day in the past)

Could anyone tell me why just adding milliseconds to the current date doesn't work?


08-24-2007, 01:03 PM
Not sure why to say the truth, but if you are looking for another way of adding a week to a defined date in JS, why not use getDate() and setDate()

check the following documentation from Mozilla.


Philip M
08-24-2007, 04:32 PM
I don't see your problem - your script works fine for me.