How to change Date Time format in asp.net

In this article I am going to explain how we can how to change DATETIME format in asp.net using C# and Vb.net.

C# code:
Display current date and time
lbldatetime.Text = DateTime.Now.ToString();
          //output 15-Mar-2016 11:40:40 AM

Display current date in long format
lbldatetime.Text = DateTime.Now.ToLongDateString();
        //output Tuesday, March 15, 2016

Display current time in long format
lbldatetime.Text = DateTime.Now.ToLongTimeString();
        //output 1:08:49 PM

Display current date in short format
lbldatetime.Text = DateTime.Now.ToShortDateString();
        //output 15-Mar-2016

Display current time in short format
lbldatetime.Text = DateTime.Now.ToShortTimeString();
        //output 1:11 PM

Display current date and time in 24 hour time format
lbldatetime.Text = DateTime.Now.ToString("dd-MM-yy HH:mm:ss");
        //output 15-03-16 13:01:24

Display current date and time in 24 hour time format with AM or PM
lbldatetime.Text = DateTime.Now.ToString("dd-MM-yy HH:mm:ss tt");
        //output 15-03-16 13:02:25 PM

Display current date and time in 12 hour time format with AM or PM
lbldatetime.Text = DateTime.Now.ToString("dd-MM-yy hh:mm:ss tt");
        //output 15-03-16 01:02:56 PM

Display current date
lbldatetime.Text = DateTime.Now.ToString("dd-MM-yyyy");
//output 15-03-2016

lbldatetime.Text = DateTime.Now.ToString("dd-MM-yy");
        //output 15-03-16
lbldatetime.Text = DateTime.Now.ToString("dd-MMMM-yy");
        //output 15-March-16

lbldatetime.Text = DateTime.Now.ToString("dd-MMM-yy");
        //output 15-Mar-16

lbldatetime.Text = DateTime.Now.ToString("yy-MM-dd");
        //output 16-03-15

lbldatetime.Text = DateTime.Now.ToString("yyyy-MMMM-dd");
        //output 2016-March-15

lbldatetime.Text = DateTime.Now.ToString("MM-dd-yy");
        //output 03-15-16

lbldatetime.Text = DateTime.Now.ToString("MM-dd-yyyy");
        //output 03-15-2016

lbldatetime.Text = DateTime.Now.ToString("MMMM-dd-yyyy");
        //output March-15-2016

Display current day and date


lbldatetime.Text = DateTime.Now.ToString("ddd dd-MM-yy");
        //output Tue 15-03-16

lbldatetime.Text = DateTime.Now.ToString("dddd dd-MM-yy");
        //output Tuesday 15-03-16

lbldatetime.Text = DateTime.Now.ToString("yyyy-MMMM-dd dddd");
        //output  2016-March-15 Tuesday

lbldatetime.Text = DateTime.Now.ToString("MMMM-dd-yyyy dddd");
        //output March-15-2016 Tuesday
  
VB.net code:
        lbldatetime.Text = DateTime.Now.ToString()
        'output 15-Mar-2016 11:40:40 AM

        lbldatetime.Text = DateTime.Now.ToLongDateString()
        'output Tuesday, March 15, 2016

        lbldatetime.Text = DateTime.Now.ToLongTimeString()
        'output 1:08:49 PM

        lbldatetime.Text = DateTime.Now.ToShortDateString()
        'output 15-Mar-2016

        lbldatetime.Text = DateTime.Now.ToShortTimeString()
        'output 1:11 PM

        lbldatetime.Text = DateTime.Now.ToString("dd-MM-yy HH:mm:ss")
        'output 15-03-16 13:01:24

        lbldatetime.Text = DateTime.Now.ToString("dd-MM-yy HH:mm:ss tt")
        'output 15-03-16 13:02:25 PM

        lbldatetime.Text = DateTime.Now.ToString("dd-MM-yy hh:mm:ss tt")
        'output 15-03-16 01:02:56 PM

        lbldatetime.Text = DateTime.Now.ToString("dd-MM-yyyy")
        'output 15-03-2016

        lbldatetime.Text = DateTime.Now.ToString("dd-MM-yy")
        'output 15-03-16
        lbldatetime.Text = DateTime.Now.ToString("dd-MMMM-yy")
        'output 15-March-16

        lbldatetime.Text = DateTime.Now.ToString("dd-MMM-yy")
        'output 15-Mar-16

        lbldatetime.Text = DateTime.Now.ToString("yy-MM-dd")
        'output 16-03-15

        lbldatetime.Text = DateTime.Now.ToString("yyyy-MMMM-dd")
        'output 2016-March-15

        lbldatetime.Text = DateTime.Now.ToString("MM-dd-yy")
        'output 03-15-16

        lbldatetime.Text = DateTime.Now.ToString("MM-dd-yyyy")
        'output 03-15-2016

        lbldatetime.Text = DateTime.Now.ToString("MMMM-dd-yyyy")
        'output March-15-2016

        lbldatetime.Text = DateTime.Now.ToString("ddd dd-MM-yy")
        'output Tue 15-03-16

        lbldatetime.Text = DateTime.Now.ToString("dddd dd-MM-yy")
        'output Tuesday 15-03-16

        lbldatetime.Text = DateTime.Now.ToString("yyyy-MMMM-dd dddd")
        'output  2016-March-15 Tuesday

        lbldatetime.Text = DateTime.Now.ToString("MMMM-dd-yyyy dddd")
        'output March-15-2016 Tuesday
  

Post a Comment

0 Comments