w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Print all possible standard date and time formats

C# Sharp DateTime: Exercise-24 with Solution

Write a C# Sharp program to display the string representation of a date using all possible standard date and time formats in the computer's current culture (en-US.).

Sample Solution:-

C# Sharp Code:

using System;
public class Example24
{
  public static void Main(String[] argv){  
    DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15, 16);

string[] july28Formats = july28.GetDateTimeFormats();

// Print out july28 in all DateTime formats using the default culture.
foreach (string format in july28Formats) {
	Console.WriteLine(format);
}
  }
}

Sample Output:

7/28/09                                                                                                      
07/28/09                                                                                                      
07/28/2009                                                                                                    
09/07/28                                                                                                      
2009-07-28                                                                                                    
28-Jul-09                                                                                                     
Tuesday, July 28, 2009                                                                                        
July 28, 2009                                                                                                 
Tuesday, 28 July, 2009                                                                                        
28 July, 2009                                                                                                 
Tuesday, July 28, 2009 5:23 AM                                                                                
Tuesday, July 28, 2009 05:23 AM                                                                               
Tuesday, July 28, 2009 5:23                                                                                   
Tuesday, July 28, 2009 05:23                                                                                  
July 28, 2009 5:23 AM                                                                                         
July 28, 2009 05:23 AM                                                                                        
July 28, 2009 5:23                                                                                            
July 28, 2009 05:23                                                                                           
Tuesday, 28 July, 2009 5:23 AM                                                                                
Tuesday, 28 July, 2009 05:23 AM  
--------
Monday, 27 July, 2009 11:53:15 PM                                                                             
Monday, 27 July, 2009 11:53:15 PM                                                                             
Monday, 27 July, 2009 23:53:15                                                                                
Monday, 27 July, 2009 23:53:15                                                                                
27 July, 2009 11:53:15 PM                                                                                     
27 July, 2009 11:53:15 PM                                                                                     
27 July, 2009 23:53:15                                                                                        
27 July, 2009 23:53:15                                                                                        
July 2009                                                                                                     
July 2009 

C# Sharp Code Editor:

using System;
public class Example24
{
  public static void Main(String[] argv){  
    DateTime july28 = new DateTime(2009, 7, 28, 5, 23, 15, 16);

string[] july28Formats = july28.GetDateTimeFormats();

// Print out july28 in all DateTime formats using the default culture.
foreach (string format in july28Formats) {
	Console.WriteLine(format);
}
  }
}

Improve this sample solution and post your code through Disqus