Please disable your adblock and script blockers to view this page
 

Date and Time formats in asp.net using C#,VB.Net | Convert date time formats

Introduction: In this article i am going to explain with example list of different date and time formats/types available in asp.net using both C# and VB.Net language.

Description: In previous related articles i explained How to Get age in years,months,days,hours and seconds from DOB in asp.net C#,Vb.Net and Formatting Date in GridView and Get current date in dd/mm/yyyy format and Date format in sql server and  How to Convert Rupees,Currency or Numbers to Words.
It is very common in asp.net application or any other application to show date and time in various formats as per application requirement. There are so many different datetime formats/type available that can be used as per application requirement.Some of them are custom date and time formats. So this example will be helpful for those who are searching for datetime formatting and converting date and time format to one format to another e.g. dd/MM/yyyy to MM/dd/yyyy and in any other.

Implementation: Let's see the different formats with example.I have listed many date time formats that will be useful for developers.

C#.Net Code to show/display list of different Date and Time formats

protected void btnDateTime_Click(object sender, EventArgs e)
    {
        Response.Write("<BR>" + DateTime.Today.ToString("dd-MM-yyyy"));  // Output : 14-08-2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd/MM/yyyy"));  // Output : 14/08/2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd MM yyyy"));  // Output : 14 08 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("dd-MM-yy"));  // Output : 14-08-13
        Response.Write("<BR>" + DateTime.Today.ToString("dd/MM/yy"));  // Output : 14/08/13
        Response.Write("<BR>" + DateTime.Today.ToString("dd MM yy"));  // Output : 14 08 13
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("ddd-dd-MM-yy"));  // Output : Wed-14-08-13
        Response.Write("<BR>" + DateTime.Today.ToString("ddd/dd/MM/yy"));  // Output : Wed/14/08/13
        Response.Write("<BR>" + DateTime.Today.ToString("ddd dd MM yy"));  // Output : Wed 14 08 13
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("dddd-dd-MM-yy"));  // Output : Wednesday-14-08-13
        Response.Write("<BR>" + DateTime.Today.ToString("dddd/dd/MM/yy"));  // Output : Wednesday/14/08/13
        Response.Write("<BR>" + DateTime.Today.ToString("dddd dd MM yy"));  // Output : Wednesday 14 08 13
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("dd-MMM-yyyy"));  // Output : 14-Aug-2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd/MMM/yyyy"));  // Output : 14/Aug/2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd MMM yyyy"));  // Output : 14 Aug 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("dd-MMMM-yyyy"));  // Output : 14-August-2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd/MMMM/yyyy"));  // Output : 14/August/2013
        Response.Write("<BR>" + DateTime.Today.ToString("dd MMMM yyyy"));  // Output : 14 August 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("MM-dd-yyyy"));  // Output : 08-14-2013
        Response.Write("<BR>" + DateTime.Today.ToString("MM/dd/yyyy"));  // Output : 08/14/2013
        Response.Write("<BR>" + DateTime.Today.ToString("MM dd yyyy"));  // Output : 08 14 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("MMM-dd-yyyy"));  // Output : Aug-14-2013
        Response.Write("<BR>" + DateTime.Today.ToString("MMM/dd/yyyy"));  // Output : Aug/14/2013
        Response.Write("<BR>" + DateTime.Today.ToString("MMM dd yyyy"));  // Output : Aug 14 2013
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Today.ToString("yyyy-MM-dd"));  // Output : 2013-08-14
        Response.Write("<BR>" + DateTime.Today.ToString("yyyy/MM/dd"));  // Output : 2013/08/14
        Response.Write("<BR>" + DateTime.Today.ToString("yyyy MM dd"));  // Output : 2013 08 14
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd h:m:s"));  // Output : 2013-08-14 5:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd h:m:s"));  // Output : 2013/08/14 5:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd h:m:s"));  // Output : 2013 08 14 5:12:30
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss"));  // Output : 2013-08-14 05:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss"));  // Output : 2013/08/14 05:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd hh:mm:ss"));  // Output : 2013 08 14 05:12:30
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd H:m:s"));  // Output : 2013-08-14 17:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd H:m:s"));  // Output : 2013/08/14 17:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd H:m:s"));  // Output : 2013 08 14 17:12:30
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));  // Output : 2013-08-14 17:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"));  // Output : 2013/08/14 17:12:30
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd HH:mm:ss"));  // Output : 2013 08 14 17:12:30
        Response.Write("<BR/>" + "------------------------");
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss tt"));  // Output : 2013-08-14 17:12:30 PM
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss tt"));  // Output : 2013/08/14 17:12:30 PM
        Response.Write("<BR>" + DateTime.Now.ToString("yyyy MM dd HH:mm:ss tt"));  // Output : 2013 08 14 17:12:30 PM

        Response.Write("<BR/>" + "------------------------");

        DateTime now = DateTime.Now;
        Response.Write("<BR/>" + now.ToString("d"));  // Output : 14-Aug-13
        Response.Write("<BR/>" + now.ToString("D"));  // Output : Wednesday, August 14, 2013
        Response.Write("<BR/>" + now.ToString("f"));  // Output : Wednesday, August 14, 2013 5:12 PM
        Response.Write("<BR/>" + now.ToString("F"));  // Output : Wednesday, August 14, 2013 5:12:30 PM
        Response.Write("<BR/>" + now.ToString("g"));  // Output : 14-Aug-13 5:12 PM
        Response.Write("<BR/>" + now.ToString("G"));  // Output : 14-Aug-13 5:12:30 PM
        Response.Write("<BR/>" + now.ToString("m"));  // Output : August 14
        Response.Write("<BR/>" + now.ToString("M"));  // Output : August 14
        Response.Write("<BR/>" + now.ToString("o"));  // Output : 2013-08-14T17:12:30.1324811+05:30
        Response.Write("<BR/>" + now.ToString("O"));  // Output : 2013-08-14T17:12:30.1324811+05:30
        Response.Write("<BR/>" + now.ToString("s"));  // Output : 2013-08-14T17:12:30
        Response.Write("<BR/>" + now.ToString("t"));  // Output : 5:12 PM
        Response.Write("<BR/>" + now.ToString("T"));  // Output : 5:12:30 PM
        Response.Write("<BR/>" + now.ToString("u"));  // Output : 2013-08-14 17:12:30Z
        Response.Write("<BR/>" + now.ToString("U"));  // Output : Wednesday, August 14, 2013 11:42:30 AM
        Response.Write("<BR/>" + now.ToString("y"));  // Output : August, 2013
        Response.Write("<BR/>" + now.ToString("Y"));  // Output : August, 2013

        Response.Write("<BR/>" + "------------------------");

        Response.Write("<BR/>" + now.ToLongDateString());  // Output : Wednesday, August 14, 2013
        Response.Write("<BR/>" + now.ToLongTimeString());  // Output : 5:12:30 PM
        Response.Write("<BR/>" + now.ToShortDateString());  // Output : 14-Aug-13
        Response.Write("<BR/>" + now.ToShortTimeString());  // Output : 5:12 PM
        Response.Write("<BR/>" + now.ToString());  // Output : 14-Aug-13 5:12:30 PM
    }

VB.Net Code to show/display list of different Date and Time formats

Protected Sub btnDateTime_Click(sender As Object, e As System.EventArgs) Handles btnDateTime.Click
        Response.Write("<BR>" & DateTime.Today.ToString("dd-MM-yyyy")) ' Output : 14-08-2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd/MM/yyyy")) ' Output : 14/08/2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd MM yyyy")) ' Output : 14 08 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("dd-MM-yy")) ' Output : 14-08-13
        Response.Write("<BR>" & DateTime.Today.ToString("dd/MM/yy")) ' Output : 14/08/13
        Response.Write("<BR>" & DateTime.Today.ToString("dd MM yy")) ' Output : 14 08 13
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("ddd-dd-MM-yy")) ' Output : Wed-14-08-13
        Response.Write("<BR>" & DateTime.Today.ToString("ddd/dd/MM/yy")) ' Output : Wed/14/08/13
        Response.Write("<BR>" & DateTime.Today.ToString("ddd dd MM yy")) ' Output : Wed 14 08 13
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("dddd-dd-MM-yy")) ' Output : Wednesday-14-08-13
        Response.Write("<BR>" & DateTime.Today.ToString("dddd/dd/MM/yy")) ' Output : Wednesday/14/08/13
        Response.Write("<BR>" & DateTime.Today.ToString("dddd dd MM yy")) ' Output : Wednesday 14 08 13
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("dd-MMM-yyyy")) ' Output : 14-Aug-2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd/MMM/yyyy")) ' Output : 14/Aug/2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd MMM yyyy")) ' Output : 14 Aug 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("dd-MMMM-yyyy")) ' Output : 14-August-2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd/MMMM/yyyy")) ' Output : 14/August/2013
        Response.Write("<BR>" & DateTime.Today.ToString("dd MMMM yyyy")) ' Output : 14 August 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("MM-dd-yyyy")) ' Output : 08-14-2013
        Response.Write("<BR>" & DateTime.Today.ToString("MM/dd/yyyy")) ' Output : 08/14/2013
        Response.Write("<BR>" & DateTime.Today.ToString("MM dd yyyy")) ' Output : 08 14 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("MMM-dd-yyyy")) ' Output : Aug-14-2013
        Response.Write("<BR>" & DateTime.Today.ToString("MMM/dd/yyyy")) ' Output : Aug/14/2013
        Response.Write("<BR>" & DateTime.Today.ToString("MMM dd yyyy")) ' Output : Aug 14 2013
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Today.ToString("yyyy-MM-dd")) ' Output : 2013-08-14
        Response.Write("<BR>" & DateTime.Today.ToString("yyyy/MM/dd")) ' Output : 2013/08/14
        Response.Write("<BR>" & DateTime.Today.ToString("yyyy MM dd")) ' Output : 2013 08 14
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd h:m:s")) ' Output : 2013-08-14 5:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd h:m:s")) ' Output : 2013/08/14 5:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd h:m:s")) ' Output : 2013 08 14 5:12:30
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd hh:mm:ss")) ' Output : 2013-08-14 05:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd hh:mm:ss")) ' Output : 2013/08/14 05:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd hh:mm:ss")) ' Output : 2013 08 14 05:12:30
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd H:m:s")) ' Output : 2013-08-14 17:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd H:m:s")) ' Output : 2013/08/14 17:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd H:m:s")) ' Output : 2013 08 14 17:12:30
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss")) ' Output : 2013-08-14 17:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss")) ' Output : 2013/08/14 17:12:30
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd HH:mm:ss")) ' Output : 2013 08 14 17:12:30
        Response.Write("<BR/>" & "------------------------")
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss tt")) ' Output : 2013-08-14 17:12:30 PM
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss tt")) ' Output : 2013/08/14 17:12:30 PM
        Response.Write("<BR>" & DateTime.Now.ToString("yyyy MM dd HH:mm:ss tt")) ' Output : 2013 08 14 17:12:30 PM
        Response.Write("<BR/>" & "------------------------")

        Dim now As DateTime = DateTime.Now
        Response.Write("<BR/>" & now.ToString("d")) ' Output : 14-Aug-13
        Response.Write("<BR/>" & now.ToString("D")) ' Output : Wednesday, August 14, 2013
        Response.Write("<BR/>" & now.ToString("f")) ' Output : Wednesday, August 14, 2013 5:12 PM
        Response.Write("<BR/>" & now.ToString("F")) ' Output : Wednesday, August 14, 2013 5:12:30 PM
        Response.Write("<BR/>" & now.ToString("g")) ' Output : 14-Aug-13 5:12 PM
        Response.Write("<BR/>" & now.ToString("G")) ' Output : 14-Aug-13 5:12:30 PM
        Response.Write("<BR/>" & now.ToString("m")) ' Output : August 14
        Response.Write("<BR/>" & now.ToString("M")) ' Output : August 14
        Response.Write("<BR/>" & now.ToString("o")) ' Output : 2013-08-14T17:12:30.1324811+05:30
        Response.Write("<BR/>" & now.ToString("O")) ' Output : 2013-08-14T17:12:30.1324811+05:30
        Response.Write("<BR/>" & now.ToString("s")) ' Output : 2013-08-14T17:12:30
        Response.Write("<BR/>" & now.ToString("t")) ' Output : 5:12 PM
        Response.Write("<BR/>" & now.ToString("T")) ' Output : 5:12:30 PM
        Response.Write("<BR/>" & now.ToString("u")) ' Output : 2013-08-14 17:12:30Z
        Response.Write("<BR/>" & now.ToString("U")) ' Output : Wednesday, August 14, 2013 11:42:30 AM
        Response.Write("<BR/>" & now.ToString("y")) ' Output : August, 2013
        Response.Write("<BR/>" & now.ToString("Y")) ' Output : August, 2013
        Response.Write("<BR/>" & "------------------------")

        Response.Write("<BR/>" & now.ToLongDateString()) ' Output : Wednesday, August 14, 2013
        Response.Write("<BR/>" & now.ToLongTimeString()) ' Output : 5:12:30 PM
        Response.Write("<BR/>" & now.ToShortDateString()) ' Output : 14-Aug-13
        Response.Write("<BR/>" & now.ToShortTimeString()) ' Output : 5:12 PM
        Response.Write("<BR/>" & now.ToString()) ' Output : 14-Aug-13 5:12:30 PM
    End Sub

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. ConversionConversion EmoticonEmoticon