In this code snippet, we will see how to get the current date without time in C#. We have multiple ways to get the current date without time as shown below,
For more details about the date format, please go through this blog String Format for DateTime In C#.
using System;
public class DateFormatExample
{
public static void Main()
{
// Get current date using DateTime.ToString() with date format
string date1 = DateTime.UtcNow.ToString("dd-MM-yyyy");
Console.WriteLine("The current date1 is {0}", date1);
// Get current date using DateTime.ToString() with the short date 'd' specifier
string date2 = DateTime.UtcNow.ToString("d");
Console.WriteLine("The current date2 is {0}", date2);
// Get current date using DateTime.ToString() with the long date 'D' specifier
string date3 = DateTime.UtcNow.ToString("D");
Console.WriteLine("The current date3 is {0}", date3);
// Get current date using DateTime.ToShortDateString()
string date4 = DateTime.UtcNow.ToShortDateString();
Console.WriteLine("The current date4 is {0}", date4);
// Get current date using DateTime.ToLongDateString()
string date5 = DateTime.UtcNow.ToLongDateString();
Console.WriteLine("The current date5 is {0}", date5);
Console.ReadKey();
}
}
Output:
The current date1 is 22-02-2021
The current date2 is 22-02-2021
The current date3 is 22 February 2021
The current date4 is 22-02-2021
The current date5 is 22 February 2021
Comments (0)