Convert Date format from MMddyyyy to dd-MM-yyyy using C# and VB.Net in ASP.Net

Last Reply one month ago By pandeyism

Posted one month ago

How to convert date in dd-MM-yyyy format from MMddyyyy in code behind using Asp.net C#.

 

 

You are viewing reply posted by: pandeyism one month ago.
Posted one month ago

Hi JanviDarji,

Refer below sample.

HTML

<asp:Label id="lblMessage" runat="server" />

Namespaces

C#

using System.Globalization;

VB.Net

Imports System.Globalization

Code

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        DateTime mmddyyy = Convert.ToDateTime(DateTime.ParseExact("11222019", "MMddyyyy", CultureInfo.InvariantCulture));
        string dateddMMyyyy = mmddyyy.ToString("dd-MM-yyyy");
        lblMessage.Text = dateddMMyyyy;
    }
}

VB.Net

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim mmddyyy As DateTime = Convert.ToDateTime(DateTime.ParseExact("11222019", "MMddyyyy", CultureInfo.InvariantCulture))
        Dim dateddMMyyyy As String = mmddyyy.ToString("dd-MM-yyyy")
        lblMessage.Text = dateddMMyyyy
    End If
End Sub

Output

22-11-2019