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 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2009
    Posts
    96
    Thanks
    11
    Thanked 0 Times in 0 Posts

    Timestamp to integers

    Hello,

    Bit of a simple question but I'm relatively new to Javascript...

    I have two timestamps that I want to subtract to find the difference in seconds. The format is minutes:seconds.milliseconds.

    I want to get rid of the milliseconds, and find the difference between the minutes and seconds.

    For example: "2:05.26 - 3:05.26" should give me "60" as there are 60 seconds between the two timestamps.

    Couldy ou give me a starting point for this?!

    Thanks,

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Here is something to easily adapt if you do not want hours.

    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 = "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(/[\.-]/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 ((ftsplt[0] == stsplt[0]) && (ftsplt[1] == stsplt[1]) && (ftsplt[2] < stsplt[2])) {
    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) {
    secsDiff = 60 + secsDiff;
    }
    if (secsDiff < 10) {
    secsDiff = "0" + secsDiff;
    }
    
    var diffTime = hrsDiff + ":" + minsDiff + ":" + secsDiff;
    document.myform.timeDiff.value = diffTime;
    
    }
    
    </script>

    "A man would do nothing, if he waited until he could do it so well that no one at all would find fault with what he has done." - Cardinal Newman
    Last edited by Philip M; 05-15-2009 at 04:57 PM. Reason: Small correction

  • #3
    Banned
    Join Date
    Mar 2009
    Posts
    248
    Thanks
    3
    Thanked 68 Times in 66 Posts
    icklechurch:
    Another approach, including input validation but that's not the great bulk of my code:

    See, 2 posts below.


    Code:
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
    <html>
    <head>
    <title>None</title>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
    <script type="text/javascript">	
    
    	function calcDifference(){
    
    		var nForm = document.forms[0];
    		var valid = !nForm['start'].value.match(/\.\d{2}$/) ? false : !nForm['stop'].value.match(/\.\d{2}$/) ? false : true;
    		var nStart = nForm['start'].value.substring(0,nForm['start'].value.length-3).split(":");
    		var nStop = nForm['stop'].value.substring(0,nForm['stop'].value.length-3).split(":");
    		var nStartRef = new Date();
    		var nStopRef = new Date();
    		nStartRef.setHours(1,nStart[0],nStart[1],0);
    		nStopRef.setHours(1,nStop[0],nStop[1],0);
    		if (valid)
    			{	
    			 valid = nStartRef.getMinutes() != nStart[0] ? false : nStopRef.getMinutes() != nStop[0] ? false : true;
    			}
    		if (!valid)
    			{
    			 alert('Invalid input');
    			 nForm['difference'].value = "";
    			 return; 
    			}
    		nForm['difference'].value = Math.abs((nStartRef - nStopRef) / 1000);		
    	}
    	
    	function init(){
    
    		document.forms[0]['calcBtn'].onclick = function()
    			{
    			 calcDifference();
    			}
    	}
    
    	navigator.appName == "Microsoft Internet Explorer" ? attachEvent('onload', init, false) : addEventListener('load', init, false);	
    
    </script>
    <style type="text/css">
    
    	.data {padding-left: 3px;}
    
    </style>
    </head>
    	<body>
    		<form action="" method="post">
    		  
    			<label>Start Time: <input type="text" name="start" size="8" class="data" value="2:05.26"></label>
    			<br><br>
    			<label>Stop Time: <input type="text" name="stop" size="8" class="data" value="3:05.26"></label>
    			<br><br>
    			<label>Difference in Seconds, ignoring hundredths: <input type="text" name="difference" size="5" class="data" readonly></label>
    			<br><br>
    			<input type="button" name="calcBtn" value="Calculate">						
    
    		</form>
    	</body>
    </html>
    Last edited by 12 Pack Mack; 05-15-2009 at 09:26 PM.

  • #4
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    a simple approach:
    Code:
    <script>
    
    function diff(s1, s2){
      var v=(s1+' '+s2).split(/\D+/);
     return (v[0]-v[3])*60 +(v[1]-v[4]);
    }
    
    alert(  diff( "3:05.26", "2:05.26" )  ); // shows:60
    </script>
    Last edited by rnd me; 05-15-2009 at 03:13 PM.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5

  • #5
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    17,730
    Thanks
    202
    Thanked 2,508 Times in 2,486 Posts
    Quote Originally Posted by rnd me View Post
    a simple approach:
    Code:
    <script>
    
    function diff(s1, s2){
      var v=(s1+' '+s2).split(/\D+/);
     return (v[0]-v[3])*60 +(v[1]-v[4]);
    }
    
    alert(  diff( "3:05.26", "2:05.26" )  ); // shows:60
    </script>

    alert( diff( "3:05.26", "2:05.26" ) ); // shows:60

    Is this supposed to be hours, minutes, seconds?
    alert( diff( "3:05", "2:06" ) ); // shows: NaN
    The OP wants his result in seconds, not minutes.

    Of course this is very succinct but the great bulk of my code consists in checking the validity of the start and finish times and formatting the output. This is missing from 12 Pack Mack's code as well. You can enter any rubbish figures and "calculate" a result.
    Last edited by Philip M; 05-15-2009 at 04:08 PM.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,184
    Thanks
    10
    Thanked 569 Times in 550 Posts
    Quote Originally Posted by Philip M View Post
    Of course this is very succinct but the great bulk of my code consists in checking the validity of the start and finish times and formatting the output. This is missing from 12 Pack Mack's code as well. You can enter any rubbish figures and "calculate" a result.
    i didn't figure a timestamp would change- expect a .00 if there were no ms.
    if it's entered by a user, then i agree it should be validated.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/1/19) IE7:0.2, IE8:6.7, IE11:7.4, IE9:3.8, IE10:4.4, FF:18.3, CH:43.6, SF:7.8, MOBILE:27.5


  •  

    Posting Permissions

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