Solved

convert standard time to military time

Posted on 2012-12-20
8
2,516 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 125 total points
ID: 38711925
0
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 125 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 125 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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 125 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
Today, the web development industry is booming, and many people consider it to be their vocation. The question you may be asking yourself is – how do I become a web developer?
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…

724 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