...

View Full Version : Converting to military



faiyth
06-10-2004, 07:53 PM
I'm allowing the user to enter in a time, and then in javascript it will take that time and validate it, then make it a military time if it's not a military time already. I wanted to make them conform to a specific time (dropdowns for hours, minutes), but everyone else is saying to allow them to enter ANY time into a free for all text box and then convert it into a military time. I have no idea how to even start this, I've wrote up the validator, but is there a quick painless way to write up something that will take any none military time and make it a military time?

For example:
1. User comes to page and enters 1:00 pm in the time field.
2. User exits that field
3. My validator checks that entered text to make sure it's a time and they didn't just type in "oaskhfjksd"
4. Time is valid so it needs to go and make sure that time is military time, if it's not, then it gets automatically turned into a military time and put back in the text box to replace the 1pm time the user entered.

Willy Duitt
06-12-2004, 04:55 PM
I'll give you a bump, but I do not have an answer to your question simply because it makes no sense to me to allow a user to enter any time format and then try to anticipate the possibilities and reformat the input to reflect the way you would like.

Regardless of how you try to predict which format a user may enter (PM 1:15, 1:15 PM, 1:15PM 1:15, 1315 ect...) a user will invariably come by and enter something you did not anticipate.

Good Luck...

Basscyst
06-13-2004, 12:15 AM
Hello,

This doesn't take into account ANY time format, as there are lots. It does take into consideration standard am \ pm time and will convert it to military time, regardless of what order it is entered (they could even enter dkjhcdjkh4:30pmklaslkj and it will change it to 16:30). If military time is entered it will ensure it is formatted as 00:00. This can probably be done a bit cleaner using regular expressions, but not by me :o.



<html>
<head>

<script type="text/javascript">

function formValid()
{
var obj=document.getElementById('stime')
var str=obj.value;
str=str.toLowerCase();
var x=str.indexOf('am');
var y=str.indexOf('pm');
var tm="";
for(i=0;i<str.length;i++)
{
var z=str.charAt(i)
var num=isNaN(z);
if(num==false&&z!=" ")
{
tm+=str.charAt(i);
}
}
if(x!=-1&&y!=-1)
{
alert('This is not a valid time!');
}
else if(tm.length>4||tm.length<3)
{
alert('This is not a valid time!');
}
else if(x!=-1)
{
switch(tm.length)
{
case 3:
var newtm="0";
for(i=0;i<3;i++)
{
if(i==1)
{
newtm+=':'
}
newtm+=tm.charAt(i);
}
tm=newtm;
break;

case 4:
var newtm=""
for(i=0;i<4;i++)
{
if(i==2)
{
if(newtm==12)
{
newtm='00';
}
newtm+=':'
}
newtm+=tm.charAt(i);
}
tm=newtm;
break;
}
}
else if(y!=-1)
{
switch(tm.length)
{
case 3:
var newtm="";
for(i=0;i<3;i++)
{
if(i==1)
{

newtm=parseInt(newtm)+12;
newtm+=':'
}
newtm+=tm.charAt(i);
}
tm=newtm;
break;

case 4:
var newtm=""
for(i=0;i<4;i++)
{
if(i==2)
{
if(newtm!='12')
{
newtm=parseInt(newtm)+12;
}
newtm+=':'
}
newtm+=tm.charAt(i);
}
tm=newtm;
break;
}
}
else
{
switch(tm.length)
{
case 3:
var newtm="0";
for(i=0;i<3;i++)
{
if(i==1)
{
newtm+=':'
}
newtm+=tm.charAt(i);
}
tm=newtm;
break;

case 4:
var newtm=""
for(i=0;i<4;i++)
{
if(i==2)
{
newtm+=':'
}
newtm+=tm.charAt(i);
}
tm=newtm;
break;
}
}
obj.value=tm;
}

</script>
</head>
<body>
<form>
<input type="text" id="stime" onblur="formValid()" />
</body>
</html>


Oops! fixed bug. :rolleyes:
Hope that helps,
Basscyst

Vladdy
06-13-2004, 01:04 AM
Date.prototype.toMilitaryString()
{ return this.hours + '' + this.minutes;
}

glenngv
06-14-2004, 10:57 AM
Date object doesn't have hours and minutes properties. It has getHours and getMinutes methods but it expects a full date, not time only.


Date.prototype.toMilitaryString()
{
return (isNaN(this)) ? NaN:this.getHours() + ':' + this.getMinutes();
}


This needs to be called like this:


function validateMilitaryTime(theField)
var d = new Date("1/1/2004 "+theField.value);
var mt = d.toMilitaryString();
if (!isNaN(mt)){
theField.value=mt;
return true;
}
else {
alert("Invalid time!");
theField.focus();
return false;
}
}

Vladdy
06-14-2004, 02:14 PM
thanks for correction, Glenn



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum