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#).
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#).
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"
As of .Net 6 you can use:
var dateOnly = DateOnly.FromDateTime(DateTime.Now);
Ref: https://docs.microsoft.com/en-us/dotnet/api/system.dateonly?view=net-6.0
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");