Solved

convert standard time to military time

Posted on 2012-12-20
8
2,282 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 74

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 74

Expert Comment

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

Did you forget about 1 AM to 11 AM?
0
Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

 
LVL 39

Expert Comment

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

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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

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

706 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

15 Experts available now in Live!

Get 1:1 Help Now