Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Converting user entered time to military

Status
Not open for further replies.

faiyth

Programmer
Nov 22, 2002
19
US
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?
 
Code:
<HTML>
<HEAD>
	<TITLE>JavaScript Clock</TITLE>
	<SCRIPT LANGUAGE=JAVASCRIPT TYPE="TEXT/JAVASCRIPT">
	<!-- Hide script from old browsers

	function showMilitaryTime() {
		if (document.theForm.showMilitary[0].checked) {
			return true
		}
		return false
	}

	function showTheHours(theHour) {
		if (showMilitaryTime() || (theHour > 0 && theHour < 13)) {
			return (theHour)
		}
		if (theHour == 0) {
			return (12)
		}
		return (theHour-12)
	}

	function showZeroFilled(inValue) {
		if (inValue > 9) {
			return ":" + inValue
		}
		return ":0" + inValue
	}

	function showAmPm() {
		if (showMilitaryTime()) {
			return ("")
		}
		if (now.getHours() < 12) {
			return (" am")
		}
		return (" pm")
	}

	function showTheTime() {
		now = new Date

		document.theForm.showTime.value = showTheHours(now.getHours()) +  showZeroFilled(now.getMinutes()) + showZeroFilled(now.getSeconds()) + showAmPm()
		setTimeout("showTheTime()",1000)
	}

	// End hiding script from old browsers -->
	</SCRIPT>	
</HEAD>
<BODY BGCOLOR=WHITE onLoad="showTheTime()">
<CENTER>
	<FORM NAME="theForm">
		<INPUT TYPE=TEXT NAME="showTime" SIZE=11><P>
		Display Military Time?
		<INPUT TYPE=RADIO NAME="showMilitary" CHECKED>Yes   
		<INPUT TYPE=RADIO NAME="showMilitary">No
	</FORM>
</CENTER>
</BODY>
</HTML>

Programming today is a race between software engineers striving to build better and bigger idiot-proof programs, and the Universe trying to produce bigger and better idiots. So far, the Universe is winning. - Rick Cook (No, I'm not Rick)

zen.gif
 
Alright great. I used that and it displays the time. but just NOW's time. I'm taking this and using it to let a user enter 4pm into a text box, when they leave that text box it's going to take the time they entered, make sure it's a time, and then turn that time into military time, then put it back in that text box. They could put ANY time in the text box.

So if they typed in:
1am -> 01:00
4:10 pm - > 16:10
1pm - > 13:00

etc etc etc.
 
Oh! Also, your script isn't showing me military time whether I click yes or no. It ONLY shows regular time with a am/pm or no am/pm. So since it's 1:38 right now, it's either show 1:38 or 1:38 (am/pm).. the correct military time is 13:38.
 
You should include some validation to make sure the input is correct, but how about this:
Code:
<script>
function standardToMilitaryTime(t){
  var d=new Date("1/1/1900 "+t);
  return d.getHours()+":"+(d.getMinutes().toString().length==1?"0"+d.getMinutes():d.getMinutes())
}
alert(standardToMilitaryTime("9:23 pm"))
</script>

Adam
while(ignorance){perpetuate(violence,fear,hatred);life=life-1};
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top