c# - How to get the AM PM value from a DateTime object

DateTime get AM PM
datetime-am-pm.aspx

<%@ Page Language="C#" AutoEventWireup="true"%>  
    
<!DOCTYPE html>      
<script runat="server">    
    protected void Button1_Click(object sender, System.EventArgs e)
    {
        //initialize a datetime variable with current time
        DateTime now = DateTime.Now;

        //get only current time from datetime object
        Label1.Text = "current time with AM PM: " + now.ToLongTimeString();

        Label1.Text += "<br /><br />";
        
        //string format to get only AM PM status from datetime object
        Label1.Text += "current time [only AM PM]: " + now.ToString("tt");
    }
</script>      
      
<html xmlns="http://www.w3.org/1999/xhtml">      
<head id="Head1" runat="server">      
    <title>c# example - datetime am pm</title>      
</head>      
<body>      
    <form id="form1" runat="server">      
    <div>      
        <h2 style="color:MidnightBlue; font-style:italic;">      
            c# example - datetime am pm
        </h2>      
        <hr width="550" align="left" color="Gainsboro" />      
        <asp:Label       
            ID="Label1"       
            runat="server"      
            Font-Size="X-Large"    
            Font-Names="Comic Sans MS"
            >      
        </asp:Label>      
        <br /><br />    
        <asp:Button       
            ID="Button1"       
            runat="server"       
            Text="get AM PM from datetime object"      
            OnClick="Button1_Click"    
            Height="40"      
            Font-Bold="true"      
            />      
    </div>      
    </form>      
</body>      
</html>
More c# examples