Time Format Specifier In C#

Introduction

The Time Format Specifier can be used to represent the Time in various ways. The representation of Time can be represented by many Time Format Specifiers. In this article I will explain two types of Time Format Specifiers that are Full date Short Time Format Specifier and the Full date Long Time Format Specifier.

The Full date Short Time ("f") Format Specifier

The Format Specifier for Full date Short Time is represented by "f" which is used to display  the combination of Full date and the short time that are separated by the blank space. We can display this in various ways by applying a different culture, for each culture the result is different; for example:

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            DateTime dt1 = new DateTime(2008, 4, 10, 6, 30, 0);

            Console.WriteLine("*****Display the Full date and Short Time with system current date and time****\n\n");

            //Display full date and short time with en-US culture

            Console.WriteLine("The default Full date short time in en-US is display as :\n" + dt.ToString("f") + "\n\n");

            //Display full date and short time with sv-SE culture

            CultureInfo culture = new CultureInfo("sv-SE");

            Console.WriteLine("The Full date short time in sv-SE Pattern is display as :\n" + dt.ToString("f", culture) + "\n\n");

            //Display full date and short time with el-GR culture

            CultureInfo culture1 = new CultureInfo("el-GR");

            Console.WriteLine("The Full date short time in el-GR Pattern is display as :\n" + dt.ToString("f", culture1) + "\n\n");

            //Display full date and short time with fr-FR culture

            Console.WriteLine("Apply a Create SpecificCulture method of CultureInfo class and display the date and time as :\n" + dt.ToString("f",CultureInfo.CreateSpecificCulture("fr-FR")) + "\n\n");

            Console.WriteLine("*****Display the Full date and Short Time with some other date and time****\n\n");

            //Display full date and short time with en-US culture

            Console.WriteLine("The default Full date short time in en-US is display as :\n" + dt1.ToString("f") + "\n\n");

            //Display full date and short time with sv-SE culture

            CultureInfo culture2 = new CultureInfo("sv-SE");

            Console.WriteLine("The Full date short time in sv-SE Pattern is display as :\n" + dt1.ToString("f", culture2) + "\n\n");

            //Display full date and short time with el-GR culture

            CultureInfo culture3 = new CultureInfo("el-GR");

            Console.WriteLine("The Full date short time in el-GR Pattern is display as :\n" + dt1.ToString("f", culture3) + "\n\n");

            //Display full date and short time with fr-FR culture

            Console.WriteLine("Apply a Create SpecificCulture method of CultureInfo class and display the date and time as :\n" + dt1.ToString("f", CultureInfo.CreateSpecificCulture("fr-FR")) + "\n\n");

        }

    }

} 


Output

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

The Full date Long Time ("F") Format Specifier

The Format Specifier for Full date long Time is represented by "F" which is used to display  the combination of Full date and the long time that are separated by the blank space. We can display this in various ways by applying a different culture, for each culture the result is different; for example:

namespace DateTimeFormatString

{

    class Program

    {

        static void Main(string[] args)

        {

            //Get the system current date

            DateTime dt = DateTime.Now;

            DateTime dt1 = new DateTime(2008, 4, 10, 6, 30, 0);

            Console.WriteLine("*****Display the Full date and Short Time with system current date and time****\n\n");

            //Display full date and short time with en-US culture

            Console.WriteLine("The default Full date short time in en-US is display as :\n" + dt.ToString("F") + "\n\n");

            //Display full date and short time with sv-SE culture

            CultureInfo culture = new CultureInfo("sv-SE");

            Console.WriteLine("The Full date short time in sv-SE Pattern is display as :\n" + dt.ToString("F", culture) + "\n\n");

            //Display full date and short time with el-GR culture

            CultureInfo culture1 = new CultureInfo("el-GR");

            Console.WriteLine("The Full date short time in el-GR Pattern is display as :\n" + dt.ToString("F", culture1) + "\n\n");

            //Display full date and short time with fr-FR culture

            Console.WriteLine("Apply a Create SpecificCulture method of CultureInfo class and display the date and time as :\n" + dt.ToString("F", CultureInfo.CreateSpecificCulture("fr-FR")) + "\n\n");

            Console.WriteLine("*****Display the Full date and Short Time with some other date and time****\n\n");

            //Display full date and short time with en-US culture

            Console.WriteLine("The default Full date short time in en-US is display as :\n" + dt1.ToString("F") + "\n\n");

            //Display full date and short time with sv-SE culture

            CultureInfo culture2 = new CultureInfo("sv-SE");

            Console.WriteLine("The Full date short time in sv-SE Pattern is display as :\n" + dt1.ToString("F", culture2) + "\n\n");

            //Display full date and short time with el-GR culture

            CultureInfo culture3 = new CultureInfo("el-GR");

            Console.WriteLine("The Full date short time in el-GR Pattern is display as :\n" + dt1.ToString("F", culture3) + "\n\n");

            //Display full date and short time with fr-FR culture

            Console.WriteLine("Apply a Create SpecificCulture method of CultureInfo class and display the date and time as :\n" + dt1.ToString("F", CultureInfo.CreateSpecificCulture("fr-FR")) + "\n\n");

        }

    }

}

Output

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

Summary

In this article I explained how to define and display the Time format strings in C#.


Similar Articles