w3resource logo
C# Sharp Programming Exercises

C# Sharp Exercises: Retrieve the current date

C# Sharp DateTime: Exercise-8 with Solution

Write a C# Sharp program to retrieve the current date.

Sample Solution:-

C# Sharp Code:

 using System;

public class Example8
{
   public static void Main()
   {
      // Get the current date.
      DateTime thisDay = DateTime.Today;
      Console.WriteLine("General format " +thisDay.ToString());
      Console.WriteLine("Display the date in a variety of formats: ");
      Console.WriteLine(thisDay.ToString("d"));
      Console.WriteLine(thisDay.ToString("D"));
      Console.WriteLine(thisDay.ToString("g"));
   }
}

Sample Output:

General format 6/10/2017 12:00:00 AM                                                                          
Display the date in a variety of formats:                                                                     
6/10/2017                                                                                                     
Saturday, June 10, 2017                                                                                       
6/10/2017 12:00 AM 

C# Sharp Code Editor:

using System;

public class Example8
{
   public static void Main()
   {
      // Get the current date.
      DateTime thisDay = DateTime.Today;
      Console.WriteLine("General format " +thisDay.ToString());
      Console.WriteLine("Display the date in a variety of formats: ");
      Console.WriteLine(thisDay.ToString("d"));
      Console.WriteLine(thisDay.ToString("D"));
      Console.WriteLine(thisDay.ToString("g"));
   }
}

Improve this sample solution and post your code through Disqus