C#: String representation of a date and time using CultureInfo objects
C# Sharp DateTime: Exercise-37 with Solution
Write a C# Sharp program to display the string representation of a date and time using CultureInfo objects that represent five different cultures.
Sample Solution:-
C# Sharp Code:
using System;
using System.Globalization;
public class Example37
{
public static void Main()
{
// Array of CultureInfo objects representing various cultures.
CultureInfo[] cultures = new CultureInfo[] {
CultureInfo.InvariantCulture, // Invariant Culture
new CultureInfo("en-ZA"), // English (South Africa)
new CultureInfo("ko-KR"), // Korean (Korea)
new CultureInfo("de-DE"), // German (Germany)
new CultureInfo("es-ES"), // Spanish (Spain)
new CultureInfo("en-US") // English (United States)
};
// A specific DateTime object
DateTime thisDate = new DateTime(2016, 5, 17, 9, 0, 0);
// Iterate through each culture and display the formatted date
foreach (CultureInfo culture in cultures)
{
string cultureName;
// Determine the culture's name to display
if (string.IsNullOrEmpty(culture.Name))
cultureName = culture.NativeName; // Use native name if no specific name
else
cultureName = culture.Name;
// Display the formatted date in the specified culture
Console.WriteLine("In {0}, {1}",
cultureName, thisDate.ToString(culture));
}
}
}
Sample Output:
In Invariant Language (Invariant Country), 05/17/2016 09:00:00 In en-ZA, 2016-05-17 09:00:00 AM In ko-KR, 2016-05-17 오전 9:00:00 In de-DE, 17.05.2016 09:00:00 In es-ES, 17/05/2016 9:00:00 In en-US, 5/17/2016 9:00:00 AM
Flowchart :
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program to convert the value of the current DateTime object to its equivalent string representation using the formatting conventions of the current culture.
Next: 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.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
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-37.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics