How to get the current date in C#?
There are multiple ways to get current date in C#, and you can format it accordingly.1) Using DateTime.Today property.
2) Using DateTime.UtcNow.Date property.
To format the date we can call built in method
ToString()
and pass the format we want.Example of getting current date in C#
In below example we are going to use both ways and printing using format: dd/MM/yyyyusing System;
public class Example
{
public static void Main()
{
DateTime today_firstWay = DateTime.Today;
DateTime today_SecondWay = DateTime.UtcNow.Date;
Console.WriteLine(today_firstWay.ToString("dd/MM/yyyy"));
Console.WriteLine(today_SecondWay.ToString("dd/MM/yyyy"));
}
}
Output
06/02/2022
06/02/2022
06/02/2022