w3resource

C#: 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
{
    // Main method, entry point of the program
    public static void Main()
    {
        // Get the current date without a time component
        DateTime thisDay = DateTime.Today;

        // Display the date in the general format using ToString() method
        Console.WriteLine("General format " + thisDay.ToString());

        // Display the date in different specific formats using ToString() method
        Console.WriteLine("Display the date in a variety of formats: ");
        Console.WriteLine(thisDay.ToString("d")); // Short date format
        Console.WriteLine(thisDay.ToString("D")); // Long date format
        Console.WriteLine(thisDay.ToString("g")); // General date and time format
    }
}

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 

Flowchart :

Flowchart: C# Sharp Exercises - Retrieve the current date

C# Sharp Code Editor:

Improve this sample solution and post your code through Disqus

Previous: Write a C# Sharp program to get the time of day from a given array of date time values.
Next: Write a C# Sharp program to calculate what day of the week is 40 days from this moment.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/csharp-exercises/datetime/csharp-datetime-exercise-8.php