?
Solved

convert standard time to military time

Posted on 2012-12-20
8
Medium Priority
?
2,725 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
8 Comments
 
LVL 20

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

 
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

[Webinar] Cloud and Mobile-First Strategy

Maybe you’ve fully adopted the cloud since the beginning. Or maybe you started with on-prem resources but are pursuing a “cloud and mobile first” strategy. Getting to that end state has its challenges. Discover how to build out a 100% cloud and mobile IT strategy in this webinar.

Question has a verified solution.

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

Having worked on larger scale sites, we found out that you are bound to look at more scalable solutions to integrating widgets, code snippets or complete applications and mesh them into functional sites, in any given composition. To share some of…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
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

839 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