Introduction:
Here
I will explain how to use jQuery
to convert 12 hours AM / PM time to 24 hours time format using jQuery
or jQuery
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, jQuery show testimonials with CSS, jQuery disable submit button to avoid duplicate form submission, jQuery Bouncing Menu Example and many articles relating to JavaScript, jQuery, asp.net. Now I will explain how to convert AM/PM time to 24 hour time using jQuery.
In previous articles I explained jQuery print div with css, jQuery get all selected checkbox values with comma separated values, jQuery show testimonials with CSS, jQuery disable submit button to avoid duplicate form submission, jQuery Bouncing Menu Example and many articles relating to JavaScript, jQuery, asp.net. Now I will explain how to convert AM/PM time to 24 hour time using jQuery.
To
implement this we need to write the code like as shown below
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>jQuery Convert AM / PM time to 24 hours time</title>
<script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
<script type="text/javascript">
$(function()
{
$('#btnConvert').click(function() {
var time = $("#txttime").val();
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>
<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" /></td>
</tr>
</table>
</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. |
|||
|
|||
1 comments :
sir please give explanation for the below code
var hrs = Number(time.match(/^(\d+)/)[1]);
var mnts = Number(time.match(/:(\d+)/)[1]);
var format = time.match(/\s(.*)$/)[1];
Note: Only a member of this blog may post a comment.