Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-05-2002, 05:02 PM   PM User | #1
mactruck
New to the CF scene

 
Join Date: Aug 2002
Location: Dallas TX Metro area
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mactruck is an unknown quantity at this point
Question Calculate Date and Time 1 hour earlier

Hi,

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".

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

Thanks.
mactruck is offline   Reply With Quote
Old 08-05-2002, 08:18 PM   PM User | #2
Roy Sinclair
Senior Coder

 
Join Date: Jun 2002
Location: Wichita
Posts: 3,880
Thanks: 0
Thanked 0 Times in 0 Posts
Roy Sinclair will become famous soon enough
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).
Roy Sinclair is offline   Reply With Quote
Old 08-05-2002, 08:41 PM   PM User | #3
mactruck
New to the CF scene

 
Join Date: Aug 2002
Location: Dallas TX Metro area
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mactruck is an unknown quantity at this point
Roy,

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.
mactruck is offline   Reply With Quote
Old 08-06-2002, 01:17 AM   PM User | #4
boywonder
Regular Coder

 
Join Date: Jun 2002
Location: New York, USA
Posts: 175
Thanks: 0
Thanked 0 Times in 0 Posts
boywonder is an unknown quantity at this point
Re: Calculate Date and Time 1 hour earlier

Quote:
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:
yourdate.setHours(yourdate.getHours()-1);

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

Hope that helps

Last edited by boywonder; 08-06-2002 at 01:20 AM..
boywonder is offline   Reply With Quote
Old 08-06-2002, 03:33 PM   PM User | #5
mactruck
New to the CF scene

 
Join Date: Aug 2002
Location: Dallas TX Metro area
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
mactruck is an unknown quantity at this point
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!!!
mactruck is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 01:23 AM.


Advertisement
Log in to turn off these ads.