How to show AM and PM with time in ASP.NET

April 26, 2014 0 Comments


You can show System time as well as universal time through DateTime class. Now, this time you want to show only am and pm text after the current time. You can use "tt" in the ToString ( ) method. Lets create an simple algorithm.
Step-1 : Take a web form into the project
Step-2 : Add two control (button and label control) onto the form window.
Step-3 : Raise button click event
Step-4 : Take single instance of DateTime class also pass "tt" parameter in ToString ( ) method.
Step-5 : Now Run your application from green triangle button or Ctrl+f5

Complete Source Code

<form id="form1" runat="server">
        <asp:Button ID="Button1"
         BackColor="#99CCFF" />  
        <asp:Label ID="Label1"
Code Behind
 protected void Button1_Click(object sender, EventArgs e)
          DateTime now = DateTime.Now;          
            Label1.Text = "current time PM AM: " + now.ToLongTimeString();
            Label1.Text += "<br /><br />";          
            Label1.Text += "current time [PM AM]: " + now.ToString("tt");

Code generate the following output
How to show AM and PM with time in ASP.NET

Jackrin Reacher

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard. Google