Introduction:
Here
I will explain how to use JavaScript
to convert 12 hours AM / PM time to 24 hours time format using JavaScript
or JavaScript convert AM/PM time to 24 hour time.
Description:
In previous articles I explained jQuery print div with css, jQuery get all selected checkbox values with comma separated values, JavaScript show single quote in string, Get LinkedIn logged in user details in JavaScript, jQuery Bouncing Menu Example and many articles relating to JavaScript, jQuery, asp.net. Now I will explain how to convert 12 hours AM / PM time to 24 hours format using JavaScript.
In previous articles I explained jQuery print div with css, jQuery get all selected checkbox values with comma separated values, JavaScript show single quote in string, Get LinkedIn logged in user details in JavaScript, jQuery Bouncing Menu Example and many articles relating to JavaScript, jQuery, asp.net. Now I will explain how to convert 12 hours AM / PM time to 24 hours format using JavaScript.
To
implement this we need to write the code like as shown below
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>JavaScript
Convert AM/PM to 24 Hours Time</title>
<script type="text/javascript">
function Converttimeformat() {
// var time =
$("#starttime").val();
var time = document.getElementById('txttime').value;
var hrs = Number(time.match(/^(\d+)/)[1]);
var mnts = Number(time.match(/:(\d+)/)[1]);
var format = time.match(/\s(.*)$/)[1];
if (format == "PM"
&& hrs < 12) hrs = hrs + 12;
if (format == "AM"
&& hrs == 12) hrs = hrs - 12;
var hours = hrs.toString();
var minutes = mnts.toString();
if (hrs < 10) hours = "0"
+ hours;
if (mnts < 10) minutes = "0"
+ minutes;
alert(hours + ":"
+ minutes);
}
</script>
</head>
<body>
<div>
<table>
<tr>
<td><b>Enter Time:</b></td>
<td><input type="text"
id="txttime"
value="10:00
PM" /></td>
</tr>
<tr>
<td></td>
<td><input type="button"
id="btnConvert"
value="Convert
to AM/PM" onclick="Converttimeformat()" /></td>
</tr>
</table>
</div>
</body>
</html>
|
For
live demo click on below button to convert time to AM /PM format
Live
Demo
|
If you enjoyed this post, please support the blog below. It's FREE! Get the latest Asp.net, C#.net, VB.NET, jQuery, Plugins & Code Snippets for FREE by subscribing to our Facebook, Twitter, RSS feed, or by email. |
|||
|
|||
3 comments :
I tried 10pm no go, 10 pm ditto, then I tried 10:00 pm and it gave 10:00 instead of 22:00. Either it is not working or I have broken it. Sorry. Steve
Thanks. buddy
Note: Only a member of this blog may post a comment.