...

View Full Version : Date object and date arithmetics problem



shmoyko
08-24-2007, 12:42 PM
Hello,

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

today.setTime(newTodayInMS);
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?

Thanks.

ess
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.
http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Predefined_Core_Objects:Date_Object

cheers,
Ess

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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum