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
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
Comment