How to convert dd/MM/yyyy Date format to MM/dd/yyyy in Asp.Net C# and VB.NET

← PrevNext →

I frequently use the jQuery Datepicker widget in my web applications to select dates in the dd/MM/yyyy format, which is widely adopted by many developers. However, there are situations where the default format needs to be adjusted to meet different regional or project requirements. In this post, I'll walk you through a simple example of how to convert the date format from dd/MM/yyyy to MM/dd/yyyy in ASP.NET, using both C# and VB.NET.

The MM/dd/yyyy date format is often required when storing dates in an SQL Server table with a date data type. Beyond database compatibility, you may encounter other scenarios where converting to this format becomes essential, such as aligning with regional standards or integrating with external applications.

In ASP.NET, we can change a date's format using the ParseExact() method of DateTime structure. The method converts a specified string (a date with time) to other date and time formats.

Syntax

DateTime.ParseExact(string s, string format, IFormatProvider provider)

The Markup and Script

In the markup section, I have a textbox and a button. I am attaching the DatePicker widget to the Input box (textbox) inside the script. I am assigning the date format dd/mm/yy.

<head runat="server">
    <link rel="stylesheet" href="https://code.jquery.com/ui/1.12.0/themes/base/jquery-ui.css">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
    <script src="https://code.jquery.com/ui/1.12.0/jquery-ui.js"></script>
</head>
<body>
    <div>
        <input type="text" id="txtDOB" placeholder="SELECT A DATE" runat="server" />
        <asp:Button Text="Submit" ID="submit" OnClick="btClick" runat="server" />
    </div>
</body>
<script>
    $(document).ready(function () {
        $('#txtDOB').datepicker({
            dateFormat: 'dd/mm/yy'  // CHANGE DATE FORMAT.
        });
    });
</script>
Code behind Procedure (C#)
using System;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class SiteMaster : System.Web.UI.MasterPage
{
    protected void btClick(Object sender, EventArgs e)
    {
        string sDate = "";

        if (!string.IsNullOrEmpty(txtDOB.Value.Trim()))
        {
            // CONVERT DATE FORMAT.
            sDate = DateTime.ParseExact( 
                txtDOB.Value, "dd/MM/yyyy", null).ToString("MM/dd/yyyy");
        }
    }
}

The 1st parameter inside ParseExact() method is the date from the input box of type text. (See the HTML above). The 2nd parameter is the format that I wish to change the date to and I have a value null as the 3rd parameter (for the provider).

Code behind Procedure (VB.NET)
Sub btClick(ByVal sender As Object, ByVal e As EventArgs)
    Dim sDate As String = ""

    If Trim(txtDOB.Value) <> "" Then
        ' CONVERT DATE FORMAT.
        sDate = DateTime.ParseExact(
            txtDOB.Value, "dd/MM/yyyy", Nothing).ToString("MM/dd/yyyy")
    End If
End Sub

Well, that's it. Finally, save the newly formatted date in your SQL Server table.

Convert Date format dd/MM/yyyy to MM/dd/yyyy in Asp.Net

← PreviousNext →