Introduction: In this article you will learn how to convert time to 24 hour time format using JQuery as shown in demo below:
Implementation:
In the default page (.aspx) write the
following code having jQuery function “ConvertTimeFormat” that converts the
time format.
<%@ Page
Language="C#"
AutoEventWireup="true"
CodeFile="Default.aspx.cs"
Inherits="_Default"
%>
<!DOCTYPE html PUBLIC
"-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
<script type="text/javascript">
function
ConvertTimeFormat() {
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("Converted
Time: " + hours + ":" +
minutes);
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<center>
<table>
<tr>
<td><b>Enter
Time:</b></td>
<td>
<asp:TextBox ID="txtTime" runat="server" Text="10:00 PM"></asp:TextBox></td>
</tr>
<tr>
<td></td>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Convert to AM/PM" OnClientClick="ConvertTimeFormat()"
/>
</td>
</tr>
</table>
</center>
</div>
</form>
</body>
</html>
0 comments:
Post a Comment