...

View Full Version : Timestamp to integers



icklechurch
05-15-2009, 11:15 AM
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,

Philip M
05-15-2009, 12:46 PM
Here is something to easily adapt if you do not want hours.


<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

12 Pack Mack
05-15-2009, 01:21 PM
icklechurch:
Another approach, including input validation but that's not the great bulk of my code: :D

See, 2 posts below. :D:D:D




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

rnd me
05-15-2009, 03:10 PM
a simple approach:


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

Philip M
05-15-2009, 03:47 PM
a simple approach:


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

rnd me
05-16-2009, 07:21 AM
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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum