Solved

convert standard time to military time

Posted on 2012-12-20
8
2,306 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 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
 
LVL 75

Expert Comment

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

Did you forget about 1 AM to 11 AM?
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In my daily work (mainly using ASP.net), I need to write a lot of JavaScript code. One of the most repetitive tasks I do are the jQuery Ajax calls. You know: (CODE) I don't know if for you it's the same, but for me is soooo tedious to write the …
This article will give core knowledge of JavaScript and will head in to your first JavaScript program. I am Durvesh Naik and I am here to deal with this series of JavaScript. I will teach you JavaScript in part wise , as its quite boring to read big…
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…

895 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now