Networking | Hardware | Software | Multimedia | System | Unix&Linux | MBA

Home>>Hardware>>Military Time

Military Time

davidwdf
06-24-2004, 03:49 PM
Is there a way to check for military time ONLY in a field?

homerUK
06-24-2004, 04:17 PM
would it be in the format

HH:MM

? If so, then I guess you could split up the string (time) and check each part...eg

the first two digits MUST have two digits... ie: 01 or 11 etc... and cannot be greater than 24

the second two digits also must have two digits and cannot be more than 59 or less than 00.

that the sort of thing??

Willy Duitt
06-24-2004, 04:43 PM
This should get you started:

Enter Military Time (only)
<input type="text" onchange="if(!this.value.match(/^([01]\d|2[0-3])(:[0-5]\d){1,2}$/))
{alert('Please enter only military time in this input');this.value=''}">

jalarie
06-24-2004, 08:17 PM
How about if you allow input of anything the user wishes and convert it to what you want:

// timefmt1.js
// Written: 2004-06-16 by James Alarie <jalarie@umich.edu>
// http://spruce.flint.umich.edu/~jalarie/
//
// Call TimeFmt1 with a time to be formatted, and format-out code.
// Valid format-out codes are:
//
// format code description example
// ----------------- ------------------------------- ----------------
// HH:MM:SS am 12-hour, colons, seconds 03:37:41 pm
// HH:MM am 12-hour, colon, no seconds 03:37 pm
// HH24:MM:SS 24-hour, colons, seconds 15:37:41
// HH24:MM 24-hour, colon, no seconds 15:37
// HH24MMSS 24-hour, no colons, seconds 153741
// HH24MM 24-hour, no colons, no seconds 1537
//
// Examples:
//
// Time_out=TimeFmt1(input_time,'hh24mmss');
//
// Notes:
// A null input time causes return of the current time.
// If the input value contains colon(s), leading zeros are not needed.
// Spaces are ignored.
// The "am" or "pm" may be upper-case, lower-case, or mixed.

function TimeFmt1(ITime,FormatO) {
TF_Input=ITime; // time to format
TF_FormatO=FormatO; // desired output format
if (!TF_Input) {
TF_Now=new Date();
TF_HH=TF_Now.getHours();
TF_MM=TF_Now.getMinutes();
TF_SS=TF_Now.getSeconds();
TF_Input=TF_HH+':'+TF_MM+':'+TF_SS;
}
if (!TF_FormatO) { TF_FormatO='HH24MMSS'; }
TF_Input=TF_Input.toLowerCase();
TF_Input=TF_Input.replace(/\ /g,'');
TF_FormatO=TF_FormatO.toLowerCase();

TF_HH='';
TF_MM='';
TF_SS='';
TF_am='am';
if ((TF_ix1=TF_Input.indexOf('am')) > -1) {
TF_Input=TF_Input.substring(0,TF_ix1);
TF_am='am';
}
if ((TF_ix1=TF_Input.indexOf('pm')) > -1) {
TF_Input=TF_Input.substring(0,TF_ix1);
TF_am='pm';
}

// HH:MM:SS:
if (TF_Input.match(/^[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}$/)) {
TF_ix1=TF_Input.indexOf(':');
TF_HH=TF_Input.substring(0,TF_ix1);
TF_Input=TF_Input.substring(TF_ix1+1);
TF_ix1=TF_Input.indexOf(':');
TF_MM=TF_Input.substring(0,TF_ix1);
TF_Input=TF_Input.substring(TF_ix1+1);
TF_SS=TF_Input;
if ((TF_am == 'am') && (TF_HH == 12)) {
TF_HH=0;
}
}
// HH:MM:
if (TF_Input.match(/^[0-9]{1,2}\:[0-9]{1,2}$/)) {
TF_ix1=TF_Input.indexOf(':');
TF_HH=TF_Input.substring(0,TF_ix1);
TF_Input=TF_Input.substring(TF_ix1+1);
TF_MM=TF_Input;
if ((TF_am == 'am') && (TF_HH == 12)) {
TF_HH=0;
}
}
// HH24:MM:SS:
if (TF_Input.match(/^[0-9]{1,2}\:[0-9]{1,2}\:[0-9]{1,2}$/)) {
TF_ix1=TF_Input.indexOf(':');
TF_HH=TF_Input.substring(0,TF_ix1);
TF_Input=TF_Input.substring(TF_ix1+1);
TF_ix1=TF_Input.indexOf(':');
TF_MM=TF_Input.substring(0,TF_ix1);
TF_Input=TF_Input.substring(TF_ix1+1);
TF_SS=TF_Input;
}
// HH24:MM:
if (TF_Input.match(/^[0-9]{1,2}\:[0-9]{1,2}$/)) {
TF_ix1=TF_Input.indexOf(':');
TF_HH=TF_Input.substring(0,TF_ix1);
TF_Input=TF_Input.substring(TF_ix1+1);
TF_ix1=TF_Input.indexOf(':');
TF_MM=TF_Input.substring(0,TF_ix1);
}
// HH24MMSS:
if (TF_Input.match(/^[0-9]{6}$/)) {
TF_HH=TF_Input.substring(0,2);
TF_MM=TF_Input.substring(2,4);
TF_SS=TF_Input.substring(4,6);
}
// HH24MM:
if (TF_Input.match(/^[0-9]{4}$/)) {
TF_HH=TF_Input.substring(0,2);
TF_MM=TF_Input.substring(2,4);
}

if ((TF_am == 'pm') && (TF_HH != 12)) {
TF_HH=TF_HH*1+12;
}
if (TF_SS == '') { TF_SS=0; }
TF_SS=TF_HH*3600+TF_MM*60+TF_SS*1;
TF_HH=Math.floor(TF_SS/3600);
TF_SS=TF_SS%3600;
TF_MM=Math.floor(TF_SS/60);
TF_SS=TF_SS%60;
if (TF_HH < 10) { TF_HH='0'+TF_HH*1; }
if (TF_MM < 10) { TF_MM='0'+TF_MM*1; }
if (TF_SS < 10) { TF_SS='0'+TF_SS*1; }

// input complete; ready for output.
TF_Out='';

if (TF_FormatO == 'hh:mm:ss am') {
if (TF_HH < 12) {
TF_am='am';
} else {
TF_am='pm';
TF_HH-=12;
}
if (TF_HH == 0) {
TF_HH=12;
}
if (TF_HH < 10) { TF_HH='0'+TF_HH*1; }
TF_Out=''+TF_HH+':'+TF_MM+':'+TF_SS+' '+TF_am;
}
if (TF_FormatO == 'hh:mm am') {
if (TF_HH < 12) {
TF_am='am';
} else {
TF_am='pm';
TF_HH-=12;
}
if (TF_HH == 0) {
TF_HH=12;
}
if (TF_HH < 10) { TF_HH='0'+TF_HH*1; }
TF_Out=''+TF_HH+':'+TF_MM+' '+TF_am;
}
if (TF_FormatO == 'hh24:mm:ss') {
TF_Out=''+TF_HH+':'+TF_MM+':'+TF_SS;
}
if (TF_FormatO == 'hh24:mm') {
TF_Out=''+TF_HH+':'+TF_MM;
}
if (TF_FormatO == 'hh24mmss') {
TF_Out=''+TF_HH+TF_MM+TF_SS;
}
if (TF_FormatO == 'hh24mm') {
TF_Out=''+TF_HH+TF_MM;
}
if (TF_Out == '') {
alert('*** Invalid Format for Output: '+TF_FormatO);
}

return TF_Out;
}

glenngv
06-25-2004, 04:48 AM
http://www.codingforums.com/showthread.php?t=40105#post209845

davidwdf
06-28-2004, 12:41 PM
Thanks everyone. This works great!

IronCode
11-13-2004, 02:08 AM
How would i implement that into my form? I have a need that the form has to format a time field (simple text field) into military time and think this will work for that, but how do i call the scritp?

<imput type="text" name="event_time" onblur="TimeFmt1">??

I'm ratehr new to JS and would like to get this field done so that i can move on with other aspects. i have been tryingt o figure someithng out all day and keep coming up empty handed.

homerUK
11-13-2004, 11:40 AM
you could try

<input type="text" name="event_time" onchange="TimeFmt1(this.value,'HH24MMSS')">

that might work?

glenngv
11-13-2004, 11:45 AM
<html>
<head>
<script type="text/javascript">

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

function validate(oForm){
//validate event time
var oTime = oForm.event_time;
if (oTime.value == ""){
alert("Event time is required.");
oTime.focus();
return false;
}
var d = new Date("1/1/2004 " + oTime.value);
var mt = d.toMilitaryString();
if (!isNaN(mt)){
oTime.value = mt;
}
else {
alert("Event time should be in military format.");
oTime.focus();
return false;
}

//...validate other fields, return false if invalid


return true; //all inputs valid
}
</script>
</head>
<body>
<form name="myForm" onsubmit="return validate(this)">

<input type="text" name="event_time" />

...other fields

<input type="submit" name="btnSubmit" value="Submit" />

</form>
</body>
</html>

davidwdf
11-15-2004, 02:33 PM
I'm getting an error @ line 6 character 34.

glenngv
11-16-2004, 04:22 AM
Sorry I didn't test what I posted. I also found other bugs so I fixed all of them.

<html>
<head>
<script type="text/javascript">

Date.prototype.toMilitaryString = function(hasLeadingZeroHour){
if (isNaN(this)) return "";
var h = this.getHours();
var m = this.getMinutes();
if (hasLeadingZeroHour){
if (h < 10) h = "0" + h;
}
if (m < 10) m = "0" + m;
return h + ":" + m;
}

function validate(oForm){
//validate event time
var oTime = oForm.event_time;
if (oTime.value == ""){
alert("Event time is required.");
oTime.focus();
return false;
}
var d = new Date("1/1/2004 " + oTime.value);
var mt = d.toMilitaryString(true);
if (mt != ""){
oTime.value = mt;
alert("Time:" + oTime.value); //debug
}
else {
alert("Event time should be in military format.");
oTime.focus();
return false;
}

//...validate other fields, return false if invalid


alert("Submitting..."); //debug

return true; //all inputs valid
}
</script>
</head>
<body>
<form name="myForm" onsubmit="return validate(this)">

<input type="text" name="event_time" />

...other fields

<input type="submit" name="btnSubmit" value="Submit" />

</form>
</body>
</html>

davidwdf
11-16-2004, 12:27 PM
Thanks man, this works great!


 

TOP

For more info

setting a minimum size
Auto Preview of Image 
Refresh with a query s
JS Validation obsolete
Javascript Pop up func
Slide show transitions
I'm a newbie...please!
how to write coding in
Advanced Debug Info 
.js file doesn't work 

News Archive

Adding Graphic To Text
re-direct form after s
pop up window controls
Clock on homepage - He
passing a param from a
Password Procection wi
document.title in same
focus onload 
Counting Wrong from Ri
Help needed on how to 

Related stories:

Borrowed Script w/ Flaw...noticed when 'Back'-ing Into OR Refreshing the Page.
Customize alert() prompt title bar?
Basic Form Validation Function
Syntax Error with For Loop
triple menu in new window...
Javascript help please
Advanced Debug Info

Copyright@2004-2005 www.zzcoke.com All Right Reserved

advanced web statistics