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 2 of 2

Thread: Time Diff

  1. #1
    New to the CF scene
    Join Date
    Feb 2009
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Time Diff

    Hey All:

    So I am new to javascript and trying to get the time difference between a set of fields I have in my .net site. I currently have a start time and an end time each time is comprised of 3 components, StartHour(asp:dropdownlist), StartMinutes(asp:dropdownlist), and StartAMPM(asp:radiobuttonlist). The end time is identical just replacing start with end.

    If possible I am trying to calculate the difference between the start and end time and then have that value placed into a asp:textbox on the page.

    Any help would be greatly appreciated!!

    Thank you in advance!!

    Paul

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,081
    Thanks
    203
    Thanked 2,542 Times in 2,520 Posts
    This is the best I can do right now. The times are in the 24-hour clock - if you require am/pm I suggest you alter the script to make the maximum input hours value = 11 and then add 12 to that value if pm is selected.

    The script will only count the difference in times within the same day - 2300 to 0100 is invalid. In other words, end time must be later than start time.

    Obviously a good deal of the script is validating that a valid number for hours/minutes has been entered.


    Code:
    <form name = "myform">
    Enter the start time in HH:MM format <input type="text" id="startTime" name="startTime" size = "6" onblur = "chkstart()" ><br>
    Enter the finish time in HH:MM format <input type="text" id="finishTime" name="finishTime" size = "6" onblur = "calcDiff()"><br>
    Total time:- <input type = "text" id = "timeDiff" name = "timeDiff" size = "6">
    </form>
    
    <script type = "text/javascript">
    
    var st;
    var stsplt;
    var ftsplt;
    var passflag;
    
    function chkstart () {
    var flag = 0;
    st = document.myform.startTime.value;
    st = st.replace(/[\.-]/,":");
    if (!/\d{1,2}:\d{2}/.test(st)) {
    flag ++;
    }
    
    stsplt = st.split(":");
    stsplt[0] = stsplt[0] * 1;
    stsplt[1] = stsplt[1] * 1;
    
    if ((stsplt[0] < 0) || (stsplt [0] > 23)) {
    flag ++;
    }
    if ((stsplt[1] < 0) || (stsplt [1] > 59)) {
    flag ++;
    }
    
    if (flag > 0) {
    alert ("Invalid time - please re-enter! ");
    flag = 0;
    document.myform.startTime.value = "";
    document.myform.startTime.focus();
    return false;
    }
    else {
    passflag = 1;
    }
    }
    
    function calcDiff () {
    if (passflag != 1) {
    alert ("You must enter a starting time!" );
    document.myform.startTime.value = "";
    document.myform.startTime.focus();
    return false;
    }
    var flag = 0;
    var ft = document.myform.finishTime.value;
    ft = ft.replace(/[\.-]/,":");
    if (!/\d{1,2}:\d{2}/.test(ft)) {
    flag ++;
    }
    ftsplt = ft.split(":");
    ftsplt[0] = ftsplt[0] * 1;
    ftsplt[1] = ftsplt[1] * 1;
    if ((ftsplt[0] < 0) || (ftsplt [0] > 23)) {
    flag ++;
    }
    if ((ftsplt[1] < 0) || (ftsplt [1] > 59)) {
    flag ++;
    }
    if (ftsplt[0] < stsplt[0]) {
    flag ++;
    }
    if ((ftsplt[0] == stsplt[0]) && (ftsplt[1] > stsplt[1])) {
    flag ++;
    }
    
    if (flag > 0) {
    alert ("Invalid time - please re-enter! ");
    flag = 0;
    document.myform.finishTime.value = "";
    document.myform.finishTime.focus();
    return false;
    }
    
    var hrsDiff = ftsplt[0] - stsplt[0];
    
    if (ftsplt[1] < stsplt[1]) {
    hrsDiff = hrsDiff -1;
    alert (hrsDiff)
    }
    
    var minsDiff = ftsplt[1] - stsplt[1];
    if (minsDiff < 0) {
    minsDiff = 60 + minsDiff;
    }
    if (minsDiff < 10) {
    minsDiff = "0" + minsDiff;
    }
    
    var diffTime = hrsDiff + ":" + minsDiff;
    document.myform.timeDiff.value = diffTime;
    
    }
    
    </script>

    BTW, the time to say "thanks" is afterwards, not beforehand which gives the impression that you take other people's unpaid assistance for granted. Or as British politician Neil Kinnock put it, "Don't belch before you have had the meal." Prefer to use "please" beforehand and if you find a response helpful then you can use the "Thank User For This Post" button.
    Last edited by Philip M; 02-16-2009 at 10:50 PM.


  •  

    Posting Permissions

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