How to get the current date without the time?

Viewed 741058

I am able to get date and time using:

DateTime now = DateTime.Now;

How can I get the current date and time separately in the DateTime format itself?

I am not using the DateTime picker dialog box in ASP.NET (C#).

14 Answers

In .Net6, you can use the new DateOnly Type :

DateOnly.FromDateTime(DateTime.Today)

I think you need separately date parts like (day, Month, Year)

DateTime today = DateTime.Today;

Will not work for your case. You can get date separately so you don't need variable today to be as a DateTimeType, so lets just give today variable int Type because the day is only int. So today is 10 March 2020 then the result of

int today = DateTime.Today.Day;

int month = DateTime.Today.Month;

int year = DateTime.Today.Year;

MessageBox.Show(today.ToString()+ " - this is day. "+month.ToString()+ " - this is month. " + year.ToString() + " - this is year");

would be "10 - this is day. 3 - this is month. 2020 - this is year"

You can use DateTime.Now.ToShortDateString() like so:

var test = $"<b>Date of this report:</b> {DateTime.Now.ToShortDateString()}";

for month

DateTime.Now.ToString("MM");

for day

DateTime.Now.ToString("dd");

for year

DateTime.Now.ToString("yyyy");
DateTime.Now.ToString("dd/MM/yyyy");
Related