Standard Date and Time Format String In C#

Introduction

By using the standard date and time format strings we can represent the date and time in various ways. Their are multiple format specifiers to format the date and time in various ways, some of them are described here.

Date Format Specifier

The date format specifier can be used to display the date in either short or long format, In other words if I use the short date format specifier "d" it will give the date in a short format and if I use the long date specifier "D" then it will give the date in a long format. For more details go to my article Date Format Specifier In C# .

Time Format Specifier

The time specifier can also be used to represent the time in a short or long format.

The Short Time "t" Format specifier

The short time will only display the hour and minutes information but not seconds information, and we can display the time information in many cultures, as in:

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            Console.WriteLine("The default time (en-US) is as :" + dt.ToString("t"));

            Console.WriteLine("The time in es-ES culture is as :"+ dt.ToString("t",CultureInfo.CreateSpecificCulture("es-ES")));

            Console.WriteLine("The time in hr-HR culture is as :" + dt.ToString("t", CultureInfo.CreateSpecificCulture("hr-HR")));

            Console.WriteLine("The time in ar-EG culture is as :" + dt.ToString("t", CultureInfo.CreateSpecificCulture("ar-EG")));

        }

    }

}

Output

Short-time-format-specifier-in-csharp.jpg

The Long Time "T" Format specifier

The long time will display the hours, minutes and seconds information, and we can display the time information in many cultures, as in:

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            Console.WriteLine("The default time (en-US) is as :" + dt.ToString("T"));

            Console.WriteLine("The time in es-ES culture is as :"+ dt.ToString("T",CultureInfo.CreateSpecificCulture("es-ES")));

            Console.WriteLine("The time in hr-HR culture is as :" + dt.ToString("T", CultureInfo.CreateSpecificCulture("hr-HR")));

            Console.WriteLine("The time in ar-EG culture is as :" + dt.ToString("T", CultureInfo.CreateSpecificCulture("ar-EG")));

        }

    }

}

Output

Long-Time-format-specifier-in-csharp.jpg

Date and Time Format Specifier

In this format specifier we can reformat both the date and time; the various format specifiers in this are described below.

Full date Short Time/Full date Long Time

For this go to my article Time Format Specifier In C#.

General Date Short Time "g" Format specifier

This format specifier "g" can be used to represent the combination of
short date ("d") and short time ("t") patterns, separated by a space as in:
 

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            Console.WriteLine("The default date and time (en-US) is as :" + dt.ToString("g"));

            Console.WriteLine("The short date and short time in es-ES culture is as :"+ dt.ToString("g",CultureInfo.CreateSpecificCulture("es-ES")));

            Console.WriteLine("The short date and short time in fr-BE culture is as :" + dt.ToString("g", CultureInfo.CreateSpecificCulture("fr-BE")));

            Console.WriteLine("The short date and short time in zh-CN culture is as :" + dt.ToString("g", CultureInfo.CreateSpecificCulture("zh-CN")));

        }

    }

}

Output

Gerenal-date-short-time-format-specifier-in-csharp.jpg

General Date Long Time "G" Format specifier

This format specifier "g" can be used to represent the combination of
short date ("d") and long time ("T") patterns, separated by a space as in:
 

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            Console.WriteLine("The default date and time (en-US) is as :" + dt.ToString("G"));

            Console.WriteLine("The short date and short time in es-ES culture is as :"+ dt.ToString("G",CultureInfo.CreateSpecificCulture("es-ES")));

            Console.WriteLine("The short date and short time in fr-BE culture is as :" + dt.ToString("G", CultureInfo.CreateSpecificCulture("fr-BE")));

            Console.WriteLine("The short date and short time in zh-CN culture is as :" + dt.ToString("G", CultureInfo.CreateSpecificCulture("zh-CN")));

        }

    }

}

Output

Gereral-date-long-time-format-specifier-in-csharp.jpg

The Month "m / or M" Format Specifier

The month format specifier can be used to display the month in various ways as in:
 

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            Console.WriteLine("The month in en-US Format : "+ dt.ToString("m",CultureInfo.CreateSpecificCulture("en-us")));                                 

            Console.WriteLine("The month in ms-MY Format : " + dt.ToString("m", CultureInfo.CreateSpecificCulture("ms-MY")));                              

        }

    }

}

Output

Month-format-specifier-in-csharp.jpg


The Year Month " y / or Y" Format Specifier

This format specifier can be used to display the year and month in various ways as in:
 

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            Console.WriteLine("The Year month in en-US format is as :"+ dt.ToString("Y",CultureInfo.CreateSpecificCulture("en-US")));                            

            Console.WriteLine("The Year month in af-ZA format is as :"+ dt.ToString("y",CultureInfo.CreateSpecificCulture("af-ZA")));                          

        }

    }

}

Output

month-yesr-format-specifier-in-csharp.jpg

Summary

In this article I explained how to display the date and time in various ways by using standard date and time format strings.

Up Next
    Ebook Download
    View all
    Learn
    View all