w3resource logo


C# Sharp Programming Exercises

C# Sharp Exercises: Date and time value by using different conventions

Secondary Nav

C# Sharp DateTime : Exercise-38 with Solution

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.

formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm", "yyyyMMdd" }

Expected Output :

English (Canada)                                                                 
   G: 5/17/2016 1:31:17 PM                                                       
   MM/yyyy: 05/2016                                                              
   MM\/dd\/yyyy HH:mm: 05/17/2016 13:31                                          
   yyyyMMdd: 20160517                                                            
                                                                                 
venska (Finland)                                                                                             
   G: 5/17/2016 1:31:17 PM                                                                                    
   MM/yyyy: 05/2016                                                                                           
   MM\/dd\/yyyy HH:mm: 05/17/2016 13:31                                                                       
   yyyyMMdd: 20160517              
   

C# Sharp Code:

using System;
using System.Globalization;
using System.Threading;
	
public class Example38
{
   public static void Main()
   {
      String[] formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm",
                           "yyyyMMdd" };
      String[] cultureNames = { "en-CA", "sv-FI" };
      DateTime date = new DateTime(2016, 5, 17, 13, 31, 17);
      foreach (var cultureName in cultureNames) {
         var culture = new CultureInfo(cultureName);
         
         Console.WriteLine(culture.NativeName);
         foreach (var format in formats)
            Console.WriteLine("   {0}: {1}", format,
                              date.ToString(format));
         Console.WriteLine();
      }
   }
}

C# Sharp Practice online:

using System;
using System.Globalization;
using System.Threading;
	
public class Example38
{
   public static void Main()
   {
      String[] formats = { "G", "MM/yyyy", @"MM\/dd\/yyyy HH:mm",
                           "yyyyMMdd" };
      String[] cultureNames = { "en-CA", "sv-FI" };
      DateTime date = new DateTime(2016, 5, 17, 13, 31, 17);
      foreach (var cultureName in cultureNames) {
         var culture = new CultureInfo(cultureName);
         
         Console.WriteLine(culture.NativeName);
         foreach (var format in formats)
            Console.WriteLine("   {0}: {1}", format,
                              date.ToString(format));
         Console.WriteLine();
      }
   }
}

Improve this sample solution and post your code through Disqus.



Join our Question Answer community to learn and share your programming knowledge.

Help the community:

PHP: Loop

JavaScript: Delete duplicates in an array

Oracle: Round the number