Current date and time Example in ASP.NET using C#

Current date and time Example in ASP.NET using C#


Posted in : ASP.Net Posted on : November 29, 2010 at 6:26 PM Comments : [ 0 ]

In this article we will introduce how can we use current date and time in ASP.NET using C#.

Show current date and time in ASP.NET using C#

In this example we will show that how can I use the current date and time in different format. We use the following string format for show date and time.

1. ToString()
2. ToLongDateString()
3. ToLongTimeString()
4. ToShortDateString()
5. ToShortTimeString()

CurrentDate.aspx (Design page):

CurrentDate.aspx(source code):

<%@ Page Title="" Language="C#" MasterPageFile="~/RoseindiaMaster.master" 
AutoEventWireup="true"
CodeFile="CurrentDate.aspx.cs" Inherits="CurrentDate" %>
<asp:Content ID="Content1" ContentPlaceHolderID="head" runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="Server">
<div>
<h2 style="color: Green">
Show current date and time in ASP.NET 4 , C#</h2>
<asp:Label Text="Show current date and time" runat="server" 
Font-Bold="True" Font-Size="Large" />
<br />
<asp:Label ID="Label1" runat="server" Font-Bold="True" 
Font-Names="Verdana" Font-Size="Small"
ForeColor="#000099"></asp:Label>
<br />
<br />
<asp:Label ID="Label2" Text="Show current date in long format" runat="server" 
Font-Bold="True" Font-Size="Large" />
<br />
<asp:Label ID="Label3" runat="server" Font-Bold="True" 
Font-Names="Verdana" Font-Size="Small"
ForeColor="#000099"></asp:Label>
<br />
<br />
<asp:Label ID="Label7" Text="Show current time in long format" runat="server" 
Font-Bold="True" Font-Size="Large" />
<br />
<asp:Label ID="Label4" runat="server" Font-Bold="True" 
Font-Names="Verdana" Font-Size="Small"
ForeColor="#000099"></asp:Label>
<br />
<br />
<asp:Label ID="Label8" Text="Show current date in short format" runat="server" 
Font-Bold="True" Font-Size="Large" />
<br />
<asp:Label ID="Label5" runat="server" Font-Bold="True" 
Font-Names="Verdana" Font-Size="Small"
ForeColor="#000099"></asp:Label>
<br />
<br />
<asp:Label ID="Label9" Text="Show current time short format" runat="server" 
Font-Bold="True" Font-Size="Large" />
<br />
<asp:Label ID="Label6" runat="server" Font-Bold="True" 
Font-Names="Verdana" Font-Size="Small"
ForeColor="#000099"></asp:Label>
</div>
</asp:Content>

CurrentDate.aspx.cs(C# code file):

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class CurrentDate : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
Label1.Text = System.DateTime.Now.ToString();
Label3.Text = System.DateTime.Now.ToLongDateString();
Label4.Text = System.DateTime.Now.ToLongTimeString();
Label5.Text = System.DateTime.Now.ToShortDateString();
Label6.Text = System.DateTime.Now.ToShortTimeString();
}
}

Output:

Download source code
Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics