Aspdotnet-Suresh

aspdotnet-suresh offers C#.net articles and tutorials,csharp dot net,asp.net articles and tutorials,VB.NET Articles,Gridview articles,code examples of asp.net 2.0 /3.5,AJAX,SQL Server Articles,examples of .net technologies

JavaScript Convert 12 Hour AM/PM Time to 24 Hours Time Format

Aug 27, 2013
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.

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


Enter Time:

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.

subscribe by rss Subscribe by RSS subscribe by email Subscribe by Email

3 comments :

Anonymous said...

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

Clansi Deena said...
This comment has been removed by a blog administrator.
Dhaval Parekh said...

Thanks. buddy

Give your Valuable Comments

Note: Only a member of this blog may post a comment.

© 2015 Aspdotnet-Suresh.com. All Rights Reserved.
The content is copyrighted to Suresh Dasari and may not be reproduced on other websites without permission from the owner.