w3resource

C#: String representation of a date and time for four different cultures

C# Sharp DateTime: Exercise-39 with Solution

Write a C# Sharp program to use each of the standard date and time format strings to display the string representation of a date and time. This is for four different cultures.

Sample Solution:-

C# Sharp Code:

using System;
using System.Globalization;

public class Example39
{
    public static void Main()
    {
        // Create an array of all supported standard date and time format specifiers.
        string[] formats = {"d", "D", "f", "F", "g", "G", "m", "o", "r", 
                            "s", "t", "T", "u", "U", "Y"};

        // Create an array of four cultures.
        CultureInfo[] cultures = {CultureInfo.CreateSpecificCulture("zh-HK"), 
                                  CultureInfo.CreateSpecificCulture("en-US"), 
                                  CultureInfo.CreateSpecificCulture("es-GB"), 
                                  CultureInfo.CreateSpecificCulture("fr-CA")};

        // Define date to be displayed.
        DateTime dateToDisplay = new DateTime(2015, 10, 16, 17, 4, 32);

        // Iterate through each standard format specifier.
        foreach (string formatSpecifier in formats)
        {
            // Iterate through each culture.
            foreach (CultureInfo culture in cultures)
            {
                // Display formatted date using the current format specifier and culture.
                Console.WriteLine("{0} Format Specifier {1, 10} Culture {2, 40}", 
                                  formatSpecifier, culture.Name, 
                                  dateToDisplay.ToString(formatSpecifier, culture));
            }

            Console.WriteLine(); // Add a line break for better readability between format specifiers.
        }   
    }
}

Sample Output:

d Format Specifier      zh-HK Culture                               16/10/2015                                
d Format Specifier      en-US Culture                               10/16/2015                                
d Format Specifier      es-ES Culture                               16/10/2015                                
d Format Specifier      fr-CA Culture                               2015-10-16                                
                                                                                                              
D Format Specifier      zh-HK Culture                              2015年10月16日                                
D Format Specifier      en-US Culture                 Friday, October 16, 2015                                
D Format Specifier      es-ES Culture           viernes, 16 de octubre de 2015                                
D Format Specifier      fr-CA Culture                          16 octobre 2015 
------
d Format Specifier      zh-HK Culture                               16/10/2015                                
d Format Specifier      en-US Culture                               10/16/2015                                
d Format Specifier      es-ES Culture                               16/10/2015                                
d Format Specifier      fr-CA Culture                               2015-10-16                                
                                                                                                              
D Format Specifier      zh-HK Culture                              2015年10月16日                                
D Format Specifier      en-US Culture                 Friday, October 16, 2015                                
D Format Specifier      es-ES Culture           viernes, 16 de octubre de 2015                                
D Format Specifier      fr-CA Culture                          16 octobre 2015 

Flowchart :

Flowchart: C# Sharp Exercises - String representation of a date and time for four different cultures

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to uses following three format strings to display a date and time value by using the conventions of the en-CA and sv-FI cultures.
Next: Write a C# Sharp program to convert the value of the current DateTime object to Coordinated Universal Time (UTC).

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/datetime/csharp-datetime-exercise-39.php