Date math problem

# Thread: Date math problem

1. jlh
Junior Member
Join Date
Dec 1969
Posts
10

## Date math problem

Â*<BR>Am I crazy, or is this this a bug in the Date object?<BR><BR>The object of this snippet of code is to take a Date object and calculate backward seven days from it.<BR><BR>CODE:<BR>============================== ============<BR><BR>Â*Â*Â*Â*var sampleDate = new Date( "4/7/2003" );<BR>Â*Â*Â*Â*document.write( "Sample date: " + sampleDate.toString() );<BR> <BR>Â*Â*Â*Â*var intMilliseconds = Date.parse( sampleDate );<BR>Â*Â*Â*Â*intMilliseconds -= 604800000;Â*Â*Â*Â*// 7 days in milliseconds<BR><BR>Â*Â*Â*Â*var adjustedDate = new Date( intMilliseconds );<BR>Â*Â*Â*Â*document.write( "A week earlier:" + adjustedDate.toString() );<BR><BR>======================================== ==<BR><BR><BR>RESULT:<BR>========================= =================<BR><BR>Â*Â*Â*Â*Sample date:<BR>Â*Â*Â*Â*Mon Apr 07 2003 00:00:00 GMT-0400 (Eastern Daylight Time)<BR><BR>Â*Â*Â*Â*A week earlier:<BR>Â*Â*Â*Â*Sun Mar 30 2003 23:00:00 GMT-0500 (Eastern Standard Time)<BR><BR>===================================== =====<BR><BR><BR>Actually, a week earlier would be Monday, March 31. It is calculating an extra day backward, as if March had only 30 days (it has 31). Any other date I&#039;ve tried with this works correctly, calculating seven days back from the date I give it.<BR><BR><BR><BR>Taking this a step further and doing a rough equivilent of it in VBScript with the same date gives the correct result:<BR><BR>CODE:<BR>========================== ================<BR><BR>Â*Â*Â*Â*theDate = CDate( "4/7/2003" )<BR>Â*Â*Â*Â*document.write "Sample date: " & theDate<BR><BR>Â*Â*Â*Â*adjDate = theDate - 7<BR>Â*Â*Â*Â*document.write "Adjusted date: " & adjDate<BR><BR>=================================== =======<BR><BR><BR>RESULT:<BR>==================== ======================<BR><BR>Â*Â*Â*Â*Sample date: 04/07/2003<BR><BR>Â*Â*Â*Â*Adjusted date: 03/31/2003<BR><BR>====================================== ====<BR><BR><BR>I&#039;ve tried this JavaScript code client-side in IE 5.5 and Mozilla 1.0.2, and server-side with scripting engine 5.6.6626 (the latest build). All gave the same results.<BR><BR>Am I doing something boneheaded here? Should I be handling date math differently than adding/subtracting millisecond values? I hope this isn&#039;t a bug in the Date object, because if I can&#039;t rely on the integrity of date math in JavaScript, I&#039;ll have to scrap it for the server-side code in my projects and go back to VBScript, which will limit me greatly (no OOP). Hopefully I am simply doing something wrong, and someone here can spot it.<BR><BR>Any insight would be appreciated.<BR>- jlh<BR>

2. jlh
Junior Member
Join Date
Dec 1969
Posts
10

## RE: Date math problem

Never mind, Steve from microsoft.public.scripting.jscript bailed me out:<BR><BR>====================================== ============<BR><BR>Congratulations, you&#039;ve just discovered the reason not to use this<BR>method--Daylight Savings Time. 168 hours before midnight, April 7 (EDT) is 11:00<BR>pm, March 30 (EST), which is what your calculation produced.<BR><BR>If you want a date that is seven calendar days previous, try<BR><BR> var sampleDate = new Date( "4/7/2003" );<BR> var adjustedDate = new Date( sampleDate );<BR> adjustedDate.setDate(adjustedDate.getDate()-7);<BR><BR> WScript.echo(sampleDate.toString()); // Mon Apr 7 00:00:00 EDT 2003<BR><BR> WScript.echo(adjustedDate.toString()); // Mon Mar 31 00:00:00 EST 2003<BR><BR>-- <BR>Steve<BR><BR>================================= =================<BR><BR>Result:<BR>1. My mistake.<BR>2. It&#039;s not a Date object bug.<BR>3. I&#039;m crazy after all.

3. Senior Member
Join Date
Dec 1969
Posts
2,225

## Not the only crazy one

I looked at your code a few times and coudldn&#039;t figure it out either because it looked like it should work.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•