PDA

View Full Version : Problem with the Date() function?


Mobius Evalon
03-27-2005, 04:00 AM
I'm trying to use javascript in conjunction with DM (backwater language similar to C) to automatically create time offsets for each user as they launch a specific form from the executable. DM provides no facility to grab time information from each individual client, so I'm forced to plug other languages into it.

The following script works peachy when the server is behind the client time (s_h < c_h) but if the server time is ahead of the client time, it gives me an off the wall result or nothing at all. I've been over it a hundred times and it seems to work when I play out the script in my head but it doesn't seem to actually -work- that way.

The object of the script is to use the Date() function to get the hour and minute data from the client, then find the hour and minute data from the hidden form object server_time, whose value is precomputed by DM and placed there, then to subtract the client time information from the server time information to create an offset that will be used later in DM after the form is submitted (for example, it's 1205 client time and 1732 server time, the offset -should- be "-5:-27").

Also, I'm using military time with a small modification (hour 0 = 24; minute 0 = 60).

function offset() {
var stamp = new Date();
var c_h = stamp.getHours();
if(c_h == 0)
{
c_h = 24;
}
var c_m = stamp.getMinutes();
if(c_m == 0)
{
c_m = 60;
}
var s_h = parseInt(document.options.server_time.value.substring(0,2));
if(s_h == 0)
{
s_h = 24;
}
var s_m = parseInt(document.options.server_time.value.substring(2,4));
if(s_m == 0)
{
s_m = 60;
}
var f_h = (c_h-s_h);
var f_m = (c_m-s_m);
if(f_h > 0 || f_m > 0)
{
document.options.form_offset.value = f_h + ':' + f_m;
}
}