View Full Version : Calculate Date and Time 1 hour earlier

08-05-2002, 06:02 PM

I am trying to create a code that will take a date and time entered by a user and calculate a date and time 1 hour prior. The code I have pieced together works most of the time, however it seems to have problems at the beginning of the month.

Here is my code:

function set_onlinedate()
ihours_in = eval(document.LogEntryForm.DHour_In.value);
iminutes_in = eval(document.LogEntryForm.DMinute_In.value);

var date_array=document.LogEntryForm.DepartureDate_In.value.split("-");
var new_date = new Date(date_array[0] ,date_array[1], date_array[2], ihours_in-1, iminutes_in, 0);
var year_out = new_date.getYear();
var month_out = new_date.getMonth();
var day_out = new_date.getDate();
var hour_out = new_date.getHours();

month_out = (month_out.toString().length < 2) ? "0" + month_out : month_out;
day_out = (day_out.toString().length < 2) ? "0" + day_out : day_out;
document.LogEntryForm.OnlineDate_In.value = year_out + '-' + month_out + '-' + day_out;
document.LogEntryForm.OHour_In.value = (hour_out.toString().length < 2) ? "0" + hour_out : hour_out;
document.LogEntryForm.OMinute_In.value = (iminutes_in.toString().length < 2) ? "0" + iminutes_in : iminutes_in;

alert("Verify that the Online Date and Time have been automatically entered correctly!");


The date and time is entered in the following format:
Date = yyyy-mm-dd
Hour = hh
Minute = mm

When I select a date of "2002-09-01" and a time of "00:15", I receive a date of "2002-08-30" and a time of "23:15". I should receive a date of "2002-08-31" and a time of "23:15".:confused:

Any help you can provide would be greatly appreciated. I am new to HTML and Java.


Roy Sinclair
08-05-2002, 09:18 PM
Did you know that the timestamp in a date field is stored as the number of milliseconds since an arbitrary time? That's the key to subtracting an hour from a given time, simply subtract the number of milliseconds in and hour from the date field. (Milliseconds per hour = 60 minutes * 60 seconds * 1000 milliseconds or 36000).

08-05-2002, 09:41 PM

Thanks for the information. I changed my code to subtract 3,600,000 (60*60*1000), but I am still getting the same date to calculate.

When I have a date and time of

09/01/2002 00:15:00

and subtract one hour, I would expect to get a date and time of

08/31/2002 23:15:00

However, I am getting a date and time of

08/30/2002 23:15:00

I have even tried with a date and time of

03/01/2002 00:15:00

and I get a date and time of

02/31/2002 00:15:00

The code seems to be correctly subtracting the one hour as I desire, but it is not calculating the correct date when I cross over the midnight time frame between to months.

Let me know I there is something else I can try. Thanks.

08-06-2002, 02:17 AM
Originally posted by mactruck
When I select a date of "2002-09-01" and a time of "00:15", I receive a date of "2002-08-30" and a time of "23:15". I should receive a date of "2002-08-31" and a time of "23:15"

I think you have your months mixed up... If you're entering numbers for the values when you create your date, remember the month array is 0 based. There is no 2002-8-31 (September 31st). September 30 is the correct date. Same goes for 2002-03-1 (April 1st) should roll back to March 31.

The easiest way to roll your date back one hour is:

that will always return a valid date 1 hour behind compensating for days months and years.

Hope that helps

08-06-2002, 04:33 PM
Thank you, Boywonder!

I had the right code in my program, but I was not aware of the month difference. When I was entering a September date I was using 9, which according to your statement meant that I was calculating based on an October date.

Thanks again!!!