In this article I will explain with an example, how to display Date in
MM/dd/yyyy format in
ASP.Net with C# and VB.Net.
HTML Markup
The
HTML Markup consists of:
Label – For displaying formatted Date in MM/dd/yyyy format.
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
Date (MM/dd/yyyy): <asp:Label ID="lblFormattedDate" runat="server" />
</form>
</body>
</html>
Namespaces
You will need to import the following namespace.
C#
using System.Globalization;
VB.Net
Imports System.Globalization
Displaying Date in MM/dd/yyyy format in ASP.Net
Inside the Page_Load event handler, the Current DateTime is set into the DateTime object and then it is formatted to the desired (MM/dd/yyyy) date with the help of ToString method.
Note: Here
ToString is passed with the desired Date Format and the Culture. Below are the details: -
MM- Month in 2 characters i.e. 02
dd – Day in 2 characters i.e. 05
yyyy – Year in 4 characters i.e. 1985
C#
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
//Get the Current Date and Time.
DateTime today = DateTime.Now;
//Format the DateTime in MM/dd/yyyy.
lblFormattedDate.Text = today.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture);
}
}
VB.Net
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles Me.Load
If Not Me.IsPostBack Then
'Get the Current Date and Time.
Dim today As DateTime = DateTime.Now
'Format the DateTime in MM/dd/yyyy.
lblFormattedDate.Text = today.ToString("MM/dd/yyyy", CultureInfo.InvariantCulture)
End If
End Sub
Screenshot
Demo
Downloads