...

View Full Version : Date() Format Function?



rfresh
05-07-2009, 04:21 PM
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

Philip M
05-07-2009, 04:46 PM
Not as easy as you might think. Try this:-



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum