?
Solved

convert standard time to military time

Posted on 2012-12-20
8
Medium Priority
?
2,597 Views
Last Modified: 2013-02-07
if I pass 6 Am it should give me militart time

ex are 6 AM or 1 pM or 12 AM
0
Comment
Question by:vjysakshi
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 19

Assisted Solution

by:Rikin Shah
Rikin Shah earned 500 total points
ID: 38711925
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 500 total points
ID: 38711944
How about:

  function convertToMilitary(value) {
    var time_regex = /(\d{1,2})(?:[: ]?(\d{2}))? *([APap][Mm])/;
    var match = time_regex.exec(value);

    if (match != null) {
        var hour = parseInt(match[1]);
        var minute = parseInt(match[2]);
        var period = match[3].toUpperCase();

        if (period == "PM" && hour < 12) {
            hour += 12;
        }
        else if (period == "AM" && hour == 12) {
            hour = 0;
        }

        if (isNaN(minute)) {
            minute = 0;
        }

        minute = "00" + minute.toString();
        hour = "00" + hour.toString();

        return hour.substring(hour.length - 2) + minute.substring(minute.length - 2);
    }
    else {
        return null;
    }
  }

Open in new window

0
 
LVL 39

Assisted Solution

by:Pratima Pharande
Pratima Pharande earned 500 total points
ID: 38712076
<script language="javascript">
function militaryTime(myItem) {
  var myValue, myArray, myHour;
  myValue = "" + myItem.value;
  myArray = myValue.split(":");
  myHour = parseInt(myArray[0], 10);
  switch (myHour) {
    case 1: myArray[0] = 13;
    break;
    case 2: myArray[0] = 14;
    break;
    case 3: myArray[0] = 15;
    break;
    case 4: myArray[0] = 16;
    break;
    case 5: myArray[0] = 17;
    break;
    case 6: myArray[0] = 18;
    break;
    case 7: myArray[0] = 19;
    break;
    case 8: myArray[0] = 20;
    break;
    case 9: myArray[0] = 21;
    break;
    case 10: myArray[0] = 22;
    break;
    case 11: myArray[0] = 23;
    break;
    case 12: myArray[0] = 24;
    break;
  };
  alert(myArray[0] + ":" + myArray[1]);
};
</script>
0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38712208
@pratima_mcs

Did you forget about 1 AM to 11 AM?
0
 
LVL 39

Expert Comment

by:Pratima Pharande
ID: 38712269
For 1AM to 11 AM there will be no change right ?
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 38712959
Yes, but your code doesn't take that into account. The switch, as written, treats 1 AM and 1 PM the same, making both 1300.
0
 
LVL 6

Assisted Solution

by:lightspeedvt
lightspeedvt earned 500 total points
ID: 38714525
Here is correct function:

function timeStandardToMilitary(time){
    return time.replace(/(\d{1,2})\s*:?\s*(\d{1,2})?\s*(am|pm)/gi, function(string, hour, minute, suffix){
        minute = minute || '00';
        return (+hour + 11)%((suffix.toLowerCase() == 'am') ? 12 : 24)+1+':'+((minute.length === 1) ? minute+'0' : minute);
    });
}

Open in new window


Demo:
http://jsfiddle.net/VZmVV/
0
 

Author Closing Comment

by:vjysakshi
ID: 38866873
good
0

Featured Post

Want to be a Web Developer? Get Certified Today!

Enroll in the Certified Web Development Professional course package to learn HTML, Javascript, and PHP. Build a solid foundation to work toward your dream job!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
JavaScript can be used in a browser to change parts of a webpage dynamically. It begins with the following pattern: If condition W is true, do thing X to target Y after event Z. Below are some tips and tricks to help you get started with JavaScript …
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…
Suggested Courses

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question