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
  1. #1
    Regular Coder
    Join Date
    Jun 2007
    Location
    Los Angeles
    Posts
    545
    Thanks
    81
    Thanked 5 Times in 5 Posts

    Date() Format Function?

    Does JS have a Date() formatting function like PHP? I'm using the JS time() function and would like to format it to display mins:secs.

    I need to get two time hacks, a start and an end time() hack and then display the diff as mins:secs.

    Thanks
    RalphF
    Business Text Messaging Services
    https://www.MobileTextingService.com

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,298
    Thanks
    203
    Thanked 2,561 Times in 2,539 Posts
    Not as easy as you might think. Try this:-

    Code:
    <form name = "myform">
    Enter the start time in HH:MM:SS format <input type="text" id="startTime" name="startTime" size = "6" onblur = "chkstart()" ><br>
    Enter the finish time in HH:MM:SS format <input type="text" id="finishTime" name="finishTime" size = "6" onblur = "calcDiff()"><br>
    Total time:- <input type = "text" id = "timeDiff" name = "timeDiff" size = "8">
    </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(/[\.-]/g,":");
    if (!/^(\d{1,2}:\d{2}:\d{2})$/.test(st)) {
    flag ++;
    }
    
    stsplt = st.split(":");
    stsplt[0] = stsplt[0] * 1;
    stsplt[1] = stsplt[1] * 1;
    stsplt[2] = stsplt[2] * 1;
    
    if ((stsplt[0] < 0) || (stsplt [0] > 23)) {
    flag ++;
    }
    if ((stsplt[1] < 0) || (stsplt [1] > 59)) {
    flag ++;
    }
    if ((stsplt[2] < 0) || (stsplt [2] > 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(/[\.-]/g,":");
    if (!/^(\d{1,2}:\d{2}:\d{2})$/.test(ft)) {
    flag ++;
    }
    ftsplt = ft.split(":");
    ftsplt[0] = ftsplt[0] * 1;
    ftsplt[1] = ftsplt[1] * 1;
    ftsplt[2] = ftsplt[2] * 1;
    
    if ((ftsplt[0] < 0) || (ftsplt [0] > 23)) {
    flag ++;
    }
    if ((ftsplt[1] < 0) || (ftsplt [1] > 59)) {
    flag ++;
    }
    if ((ftsplt[2] < 0) || (ftsplt [2] > 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;
    }
    
    var minsDiff = ftsplt[1] - stsplt[1];
    if (minsDiff < 0) {
    minsDiff = 60 + minsDiff;
    }
    if (minsDiff < 10) {
    minsDiff = "0" + minsDiff;
    }
    
    var secsDiff = ftsplt[2] - stsplt[2];
    if (secsDiff < 0) {
    secdsDiff = 60 + secsDiff;
    }
    if (secsDiff < 10) {
    secsDiff = "0" + secsDiff;
    }
    var diffTime = hrsDiff + ":" + minsDiff + ":" + secsDiff;;
    document.myform.timeDiff.value = diffTime;
    
    }
    
    </script>

    You can easily modify this if hours are not required - only minutes and seconds.


    "Ronaldo can pull anything out of a hat at the drop of a whim". - Football commentator.


  •  

    Posting Permissions

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