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 12 of 12

Thread: Military Time

  1. #1
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Military Time

    Is there a way to check for military time ONLY in a field?

  • #2
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    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??

  • #3
    Banned
    Join Date
    Sep 2003
    Posts
    3,620
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This should get you started:

    Code:
    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=''}">
    Last edited by Willy Duitt; 06-24-2004 at 04:49 PM.

  • #4
    Regular Coder
    Join Date
    Jun 2002
    Location
    Flint, Michigan, USA
    Posts
    620
    Thanks
    1
    Thanked 20 Times in 20 Posts
    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;
    }

  • #5
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts

    Similar to jalarie's suggestion...


  • #6
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks everyone. This works great!

  • #7
    New Coder
    Join Date
    Jun 2003
    Posts
    79
    Thanks
    0
    Thanked 0 Times in 0 Posts
    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.

  • #8
    Regular Coder
    Join Date
    Nov 2002
    Location
    Manchester, UK
    Posts
    533
    Thanks
    4
    Thanked 1 Time in 1 Post
    you could try

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

    that might work?

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Code:
    <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>

  • #10
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Error

    I'm getting an error @ line 6 character 34.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,042
    Thanks
    0
    Thanked 250 Times in 246 Posts
    Sorry I didn't test what I posted. I also found other bugs so I fixed all of them.
    Code:
    <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>

  • #12
    New Coder
    Join Date
    Oct 2002
    Posts
    19
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks man, this works great!


  •  

    Posting Permissions

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