w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Display the DateTime value in the formatted output

C# Sharp DateTime: Exercise-1 with Solution

Write a C# Sharp program to extract the Date property and display the DateTime value in the formatted output.

Sample Solution:-

C# Sharp Code:

using System;
public class Example1
{
   public static void Main()
   {
      DateTime dt1 = new DateTime(2016, 6, 8, 11, 49, 0);
      Console.WriteLine("Complete date: "+dt1.ToString());

      // Get date-only portion of date, without its time.
      DateTime dateOnly = dt1.Date;
      
      Console.WriteLine("Short Date: "+dateOnly.ToString("d"));
      
      Console.WriteLine("Display date using 24-hour clock format:");
      
      Console.WriteLine(dateOnly.ToString("g"));
      Console.WriteLine(dateOnly.ToString("MM/dd/yyyy HH:mm"));   
   }
}

Sample Output:

Complete date: 6/8/2016 11:49:00 AM                                                                           
Short Date: 6/8/2016                                                                                          
Display date using 24-hour clock format:                                                                      
6/8/2016 12:00 AM                                                                                             
06/08/2016 00:00 

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus