...

View Full Version : Problem with Daylight Savings and Date/Time maybe?



EdScript
03-31-2003, 06:34 PM
Help,

I wrote this function below to compute/return the end date / time when passed a start date / time and duration. It works OK all year except today when I pass it :

start_date
"04/06/03"
start_time
"02:00"
add_time
"00:00:15"

It supposed to return '04/06/02_0215'
but it's not and is returning: '04/06/02_0115'

I'm sure it has to do with daylight savings time but don't know how to correct it, please help.

Thanks,
Ed

/*--------------------------------------------------------------*/
/* FUNCTION FUNCTADD */
/* */
/* Adds a time to a start date and returns a date as the result */
/* start date - MM/DD/YY */
/* start time - HH:MM */
/* add time - DD:HH:MM */
/* returned - MM/DD/YY_HH:MM */
/* */
/* Example: */
/* var start_date = '12/30/01' */
/* var start_time = '23:00' */
/* var add_time = '01:02:15' */
/* */
/* end_date = time_add(start_date,start_time,add_time) */
/* */
/* end_date will be '01/01/02_0115' */
/* */
/*--------------------------------------------------------------*/

function time_add(start_date,start_time,add_time)
{
//alert(start_date+' '+start_time+' '+' '+add_time);

if(start_date.toUpperCase() == 'NOW')
{
var S_date = new Date() ;
}
else
{
var s_mon = start_date.substr(0,2) ;
var s_mon = s_mon.valueOf() - 1 ;
var s_day = start_date.substr(3,2) ;
var s_day = s_day.valueOf() -0 ;
var s_yr = start_date.substr(6,2) ;
var s_yr = s_yr.valueOf() - 0 ;
var s_yr = s_yr +2000 ;
var s_hr = start_time.substr(0,2) ;
var s_hr = s_hr.valueOf() - 0 ;
var s_min = start_time.substr(3,2) ;
var s_min = s_min.valueOf() - 0;
var S_date = new Date(s_yr,s_mon,s_day,s_hr,s_min) ;
}

var add_days = add_time.substr(0,2) - 0
var add_hrs = add_time.substr(3,2) - 0
var add_mins = add_time.substr(6,2) - 0

//alert('add_day/hrs/mins = '+add_days+'/'+add_hrs+'/'+add_mins)
var tot_hrs = add_days*24 + add_hrs
var tot_mins = tot_hrs*60 + add_mins
var tot_ms = tot_mins*60000
//alert('tot hrs mins ms '+tot_hrs + ' '+tot_mins+' '+tot_ms)
//alert('Start date = '+S_date)
E_date = new Date(S_date.valueOf() + tot_ms)
//alert('End date = '+E_date)

var mm = E_date.getMonth() + 1
var dd = E_date.getDate()
var yy = E_date.getFullYear() - 2000
var hr = E_date.getHours()
var mn = E_date.getMinutes()

if (mm <=9)
var mm = ("0" + mm + "/");
else
var mm = (mm + "/");
if (dd <=9)
var dd = ("0" + dd + "/");
else
var dd = (dd + "/");
if (yy <=9)
var yy = ("0" + yy);

if (hr <=9)
var hr = ("0" + hr + ":");
else
var hr = (hr + ":");
if (mn <=9)
var mn = ("0" + mn + "");
else
var mn = (mn + "");

new_date = mm+dd+yy+'_'+hr+mn
// alert('New date = '+new_date)

return new_date

} // END function time_add

jalarie
03-31-2003, 07:53 PM
Unfortunately, your routine works perfectly. It is giving you exactly what your clock will say on the target date and time. To get the answer that you prefer, you'll have to do the math by hand rather than using the Date function.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum