...

# Calculate the difference btw 2 dates

sharil_hashim
06-15-2002, 08:21 AM
Here is the script to calculate the difference between 2 dates:
the date format is in this form; date=dd/mm/yyyy and time=hh:mm
the difference will be in the form of hours.minutes

<script language=javascript>
function daysDiff(eD, eT, sD, sT) {
var endDate = eD.split("/");
var dy2 = endDate[0];
var mo2 = endDate[1];
var yr2 = endDate[2];
var startDate = sD.split("/");
var dy1 = startDate[0];
var mo1 = startDate[1];
var yr1 = startDate[2];
var endTime = eT.split(":");
var hr2 = endTime[0];
var mn2 = endTime[1];
var startTime = sT.split(":");
var hr1 = startTime[0];
var mn1 = startTime[1];
return diff = daysElapsed(new Date(yr2,mo2,dy2,hr2,mn2),new Date(yr1,mo1,dy1,hr1,mn1));
}

function y2k(number) { return (number < 1000) ? number + 1900 : number; }
function daysElapsed(date1,date2) {
var difference =
Date.UTC(y2k(date1.getYear()),date1.getMonth(),date1.getDate(),date1.getHours(),date1.getMinutes(),0 )
- Date.UTC(y2k(date2.getYear()),date2.getMonth(),date2.getDate(),date2.getHours(),date2.getMinutes(),0 );
var retVal = difference/1000/60;
return retVal;
}
</script>

However i found that for following date the calculation is not correct
>31/12/2001 12:00 - 01/01/2002 12:00 = 24 hours (correct)
>31/01/2002 12:00 - 01/02/2002 12:00 = -48 hours (incorrect)
>28/02/2002 12:00 - 01/03/2002 12:00 = 96 hours (incorrect)
>31/03/2002 12:00 - 01/04/2002 12:00 = 0 hours (incorrect)
>30/04/2002 12:00 - 01/05/2002 12:00 = 48 hours (incorrect)
>31/05/2002 12:00 - 01/06/2002 12:00 = 0 hours (incorrect)
>30/06/2002 12:00 - 01/07/2002 12:00 = 48 hours (incorrect)
>31/07/2002 12:00 - 01/08/2002 12:00 = 24 hours (correct)
>31/08/2002 12:00 - 01/09/2002 12:00 = 0 hours (incorrect)
>30/09/2002 12:00 - 01/10/2002 12:00 = 48 hours (incorrect)
>31/10/2002 12:00 - 01/11/2002 12:00 = 0 hours (incorrect)
>30/11/2002 12:00 - 01/12/2002 12:00 = 48 hours (incorrect)
>31/12/2002 12:00 - 01/01/2003 12:00 = 24 hours (correct)

if anyone have ever found solution for this problem please update this script. Thank you

joh6nn
06-15-2002, 10:41 AM
the following method will return the number of milliseconds between january first, 1970, and a date.

Date.parse(dateString);

if you take your two dates, parse() them, and then divide by 1000, and then divide by 60, you'll have the number of minutes between the two dates.

so,
function daysDiff(date1, date2) {
temp1 = date1.toString():
temp2 = date2.toString();
temp1 = Date.parse(temp1);
temp2 = Date.parse(temp2);
temp1 /= 1000; temp1 /= 60;
temp2 /= 1000; temp2 /= 60;
var temp3 = Math.abs( temp1 - temp2):
return temp3;
}
that will give you then number of minutes between the dates. to get the number of hours, you just divide by 60 again.

sharil_hashim
06-17-2002, 03:39 AM
To get the difference between the 2 dates is not a problem. However if we refer back to the main question, the problem occurs when i want to get the differences between dates of different months. Please refer to the example inthe main question.

Thank you,
Sharil Md Hashim

joh6nn
06-17-2002, 09:19 AM
::sigh::

i'm pretty sure that your problem is composed of multiple things.

first, you're using Date.getYear(), and then trying to convert it for Y2K, which won't work. there's a Y2K method name getFullYear(), which you should use instead. also, you've forgotten to account for the possibility of subtracting a larger date from a smaller date, which would result in a negative number. you have to use Math.abs() to make sure that the number is always positive.

all of that, is in the function that i wrote for you.

sharil_hashim
06-17-2002, 11:42 AM
Hi..

thanks mate.. i've found the solution. Somehow i found that the months value i pass into the variables MO1 & MO2 must be subsracted by 1. Example:
before:
daysElapsed(new Date(yr2,MO2,dy2,hr2,mn2),new Date(yr1,MO1,dy1,hr1,mn1));
after:
daysElapsed(new Date(yr2,MO2-1,dy2,hr2,mn2),new Date(yr1,MO1-1,dy1,hr1,mn1));

otherwise the month it calculate would be the month after the month i requested. Well thanks again for your help. if you have any curiosity just mail me k.

Thanks again :)

regards,
Sharil Md Hashim

jalarie
06-18-2002, 02:47 PM
What happens when the two dates and times span the Daylight Savings switch points?

sharil_hashim
06-19-2002, 03:51 AM
Well, frankly speaking, living in tropical country i never experience Daylight Saving time. In fact this is the first time i heard about it.

From my understanding it's just a matter of moving you clock 1 hour foward during summer rite?(correct me if i'm wrong!!). Therefore i think maybe we can add 1 hour extra for those period which falls; i.e. in United States at 2 a.m. on the first Sunday of April and reverts to standard time at 2 a.m. on the last Sunday of October.

Hope this will help & i don't dare to say much as i might giving u the wrong info..

regards,
Sharil Md Hashim :)

jalarie
06-19-2002, 03:31 PM
My DST question was to alert people to the fact that the result will probably be too high or too low by one hour when crossing the DST switch points. The offered scripts need to be aware of this to avoid giving incorrect results.

sharil_hashim
06-20-2002, 03:15 AM
Well that is understandable. Why not u give me the details on DST and maybe i can add up the features into the script or if u have such scripts, do mind sharing it with me.