Reader Level:
ARTICLE

Date Format Specifier In C#

Posted by Richa Garg Articles | C# Language October 23, 2012
In this article I will explain how to use the Date Format Specifier in C#.
  • 0
  • 0
  • 4796

Introduction

The Date Format Specifier can be used to represent a date in various ways. Representation of a date can be done by one of two Date Format Specifiers, that is a Short Date Format Specifier or a Long Date Format Specifier.

The Short Date ("d") Format Specifier

The Format Specifier for a short date is "d" which is used to display  the date as a short pattern, we can use various cultures so that the format of dates displayed will vary for each culture value. The default culture is "en-US" which is used to display the date in the format "mm/dd/yyyy" , the "fr-FR" culture is used to display the date in the format of "dd/mm/yyyy" and the "ja-JP" culture is used to dispaly the date in the "yyyy/mm/dd" format. Now we can better understand this concept by implementing this in a console application and see the output. In this example I use a system current date using the DateTime.Now method and display this date in various format strings; see:

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            //Display the date with the default culture

            Console.WriteLine("The date default pattern (en-US) denotes date in the format of mm/dd/yyyy : \n " + dt.ToString("d") + "\n\n");

            //Display date when apply the culture fr-FR

            CultureInfo cultureInfo = new CultureInfo("fr-FR");

            Console.WriteLine("The date pattern (fr-FR) denotes date in the format of dd/mm/yyyy : \n " + dt.ToString("d", cultureInfo) + "\n\n");

            //Display date when apply the culture ja-JP

            CultureInfo cultureInfo1 = new CultureInfo("ja-JP");

            Console.WriteLine("The date pattern (ja-JP) denotes date in the format of yyyy/mm/dd : \n " + dt.ToString("d", cultureInfo1) + "\n\n");

            //Display date when apply the Specific Culture de-DE

            Console.WriteLine("The date pattern (de-DE) denotes date in the format of mm.dd.yyyy : \n " + dt.ToString("d", CultureInfo.CreateSpecificCulture("de-DE")));

        }

    }

}

Output

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

Now if we want to display a particular date or some other date not the current date in various formats, so for this we specify that date and display it with a different Format Specifier as:
 

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get any date

            DateTime dt = new DateTime(2008, 4, 10);

            //Display the date with the default culture

            Console.WriteLine("The date default pattern (en-US) denotes date in the format of mm/dd/yyyy : \n " + dt.ToString("d") + "\n\n");

            //Display date when apply the culture fr-FR

            CultureInfo cultureInfo = new CultureInfo("fr-FR");

            Console.WriteLine("The date pattern (fr-FR) denotes date in the format of dd/mm/yyyy : \n " + dt.ToString("d", cultureInfo) + "\n\n");

            //Display date when apply the culture ja-JP

            CultureInfo cultureInfo1 = new CultureInfo("ja-JP");

            Console.WriteLine("The date pattern (ja-JP) denotes date in the format of yyyy/mm/dd : \n " + dt.ToString("d", cultureInfo1) + "\n\n");

            //Display date when apply the Specific Culture de-DE

            Console.WriteLine("The date pattern (de-DE) denotes date in the format of mm.dd.yyyy : \n " + dt.ToString("d", CultureInfo.CreateSpecificCulture("de-DE")));

        }

    }

}

In the above example only the date is different, so the output will be similar with the other one above except the different display specifier is applied to a different date.

Now if I want to see how many types I can display the short date Format Specifier, I can use the GetAllDateTimePatterns method of the DateTimeFormatInfo class as:
 

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            Console.WriteLine("'d' standard format string:");

            foreach (var customString in DateTimeFormatInfo.CurrentInfo.GetAllDateTimePatterns('d'))

                Console.WriteLine(" {0}", customString);

        }

    }

}

Output

Different-ways-to-presents-short-date-in-asharp.jpg

The Long Date ("D") Format Specifier

The Format Specifier for long date is "D" which is used to display the date as a long pattern. In this also we can use many different cultures so that the date will display in different formats. In this the date is displayed by specifying the month name as a word, also the day of that date. The following example shows how to display the date as a long pattern with various cultures:
 

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            //Get any different date by datetime

            DateTime dt1 = new DateTime(2008, 4, 10);

            Console.WriteLine("**********Display the long date pattern with system current date************\n\n");

            //Display the long default date pattern "en-US"

            Console.WriteLine("The default long date pattern : \n" + dt.ToString("D") + "\n\n");

            //Define a culture "de-DE" and display its date format

            CultureInfo culture1 = new CultureInfo("de-DE");

            Console.WriteLine("The long date pattern with de-DE culture : \n" + dt.ToString("D", culture1) + "\n\n");

            //define a new culture with Create SpecificCulture method of CultureInfo class and display its long date pattern

            Console.WriteLine("The long Date pattern with pt-BR culture : \n" + dt.ToString("D", CultureInfo.CreateSpecificCulture("pt-BR")) + "\n\n");

            Console.WriteLine("The long date pattern with es-MX culture : \n" + dt.ToString("D", CultureInfo.CreateSpecificCulture("es-MX")) + "\n\n");

            Console.WriteLine("**********Display the long date pattern with some other date************\n\n");

            //Display the long default date pattern "en-US"

            Console.WriteLine("The default long date pattern : \n" + dt1.ToString("D") + "\n\n");

            //Define a culture "de-DE" and display its date format

            CultureInfo culture = new CultureInfo("de-DE");

            Console.WriteLine("The long date pattern with de-DE culture : \n" + dt1.ToString("D", culture) + "\n\n");

            //define a new culture with Create SpecificCulture method of CultureInfo class and display its long date pattern

            Console.WriteLine("The long Date pattern with pt-BR culture : \n" + dt1.ToString("D", CultureInfo.CreateSpecificCulture("pt-BR")) + "\n\n");

            Console.WriteLine("The long date pattern with es-MX culture : \n" + dt1.ToString("D", CultureInfo.CreateSpecificCulture("es-MX")));

        }

    }

}

Output

Long-date-format-specifier-in-asharp.jpg

Summary

In this article I explained how to use Date Format Specifiers in C#.

COMMENT USING

Trending up