w3resource logo


C# Sharp Programming Exercises

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

Secondary Nav

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.

Expected 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:

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"));   
   }
}

C# Sharp Practice online:

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"));   
   }
}

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:

Python: Fizzbuzz

C++: Decimal to binary conversion

JavaScript: Need Help in JavaScript

Python: Help me with this program