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 to the CF scene
    Join Date
    Jun 2004
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Converting to military

    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.

  • #2
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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...

  • #3
    Smokes a Lot
    Join Date
    Jul 2003
    Location
    CA, USA
    Posts
    1,594
    Thanks
    5
    Thanked 20 Times in 20 Posts
    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 .

    Code:
    <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>
    Edit: Oops! fixed bug.

    Hope that helps,
    Basscyst
    Last edited by Basscyst; 06-13-2004 at 01:30 AM.
    Helping to build a bigger box. - Adam Matthews

  • #4
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Code:
    Date.prototype.toMilitaryString()
      { return this.hours + '' + this.minutes;
      }
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,075
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Date object doesn't have hours and minutes properties. It has getHours and getMinutes methods but it expects a full date, not time only.
    Code:
    Date.prototype.toMilitaryString()
    {    
        return (isNaN(this)) ? NaN:this.getHours() + ':' + this.getMinutes();
    }
    This needs to be called like this:
    Code:
    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;
        }
    }
    Last edited by glenngv; 06-14-2004 at 11:59 AM.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #6
    Senior Coder
    Join Date
    Jun 2002
    Location
    Nashua, NH
    Posts
    1,724
    Thanks
    0
    Thanked 0 Times in 0 Posts
    thanks for correction, Glenn
    Vladdy | KL
    "Working web site is not the one that looks the same on common graphical browsers running on desktop computers, but the one that adequately delivers information regardless of device accessing it"


  •  

    Posting Permissions

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