Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 9 of 9
  1. #1
    New to the CF scene
    Join Date
    Jun 2002
    Location
    Kuala Lumpur
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Calculate the difference btw 2 dates

    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
    as cool as ice

  • #2
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #3
    New to the CF scene
    Join Date
    Jun 2002
    Location
    Kuala Lumpur
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    difference btw 2 dates

    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
    as cool as ice

  • #4
    Senior Coder joh6nn's Avatar
    Join Date
    Jun 2002
    Location
    72° W. 48' 57" , 41° N. 32' 04"
    Posts
    1,887
    Thanks
    0
    Thanked 1 Time in 1 Post
    ::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.
    bluemood | devedge | devmo | MS Dev Library | WebMonkey | the Guide

    i am a loser geek, crazy with an evil streak,
    yes i do believe there is a violent thing inside of me.

  • #5
    New to the CF scene
    Join Date
    Jun 2002
    Location
    Kuala Lumpur
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile Yes.. found the solution

    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
    as cool as ice

  • #6
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    627
    Thanks
    1
    Thanked 20 Times in 20 Posts
    What happens when the two dates and times span the Daylight Savings switch points?

  • #7
    New to the CF scene
    Join Date
    Jun 2002
    Location
    Kuala Lumpur
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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
    as cool as ice

  • #8
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    627
    Thanks
    1
    Thanked 20 Times in 20 Posts
    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.

  • #9
    New to the CF scene
    Join Date
    Jun 2002
    Location
    Kuala Lumpur
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.
    as cool as ice


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •