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:
ihours_in = eval(document.LogEntryForm.DHour_In.value);
iminutes_in = eval(document.LogEntryForm.DMinute_In.value);
var new_date = new Date(date_array ,date_array, date_array, 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".
Any help you can provide would be greatly appreciated. I am new to HTML and Java.