How to Convert Date in “dd/MM/yyyy” Format to “MM/dd/yyyy” in Asp.Net C# and Vb.Net

I often use the jQuery Datepicker widget in my web application to choose a date in “dd/mm/yyyy” format. This is a very common date format used by many web developers like me. However, we may sometimes require changing the format to other date formats too. Here is this post I’ll show you a simple example on how to change the date format “dd/MM/yyyy” to “MM/dd/yyyy” date format in Asp.Net using C# and Vb.Net.

The date format MM/dd/yyyy becomes necessary when you want to save the date in an SQL Server table, with date type. You may have other reasons too.

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.


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="">
    <script src=""></script>
    <script src=""></script>
        <input type="text" id="txtDOB" placeholder="SELECT A DATE" runat="server" />
        <asp:Button Text="Submit" ID="submit" OnClick="btClick" runat="server" />
    $(document).ready(function () {
            dateFormat: 'dd/mm/yy'  // CHANGE DATE FORMAT.
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 markup). 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
        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.

Previous - Programmatically Create and Add DropDownList to a GridView Control in Asp.Net – C# and VbNext - Programmatically Create a Table in SQL Server using Asp.Net C# and Vb.Net

Like this Article? Subscribe now, and get all the latest articles and tips, right in your inbox.

Enter your email id

Delivered by FeedBurner
Tweet this article Facebook Google+

Related Posts:

Join our Google Plus Community and be a part of a discussion!