Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get today's date at midnight or no minutes and seconds

How can I get the current date but at midnight in a date object instead of a string object?

I know I can do this Date.Now.ToShortDateString, but that returns a string object. But if I do this Convert.ToDateTime(Date.Now.ToShortDateString), it seems like too much code. Let me know if I should just get over this and use it.

Reason, I need to compare two dates and the date I'm comparing it to only has a date and no time.

like image 449
FarFigNewton Avatar asked Dec 09 '22 09:12

FarFigNewton


2 Answers

There is a DateTime.Today field specifically for that purpose.
You can also use DateTime.Date property to get just the date of any datetime object, so DateTime.Now.Date will also work.

like image 97
SWeko Avatar answered Jan 07 '23 21:01

SWeko


Look at the Date property:

DateTime.Now.Date
like image 31
Jim Mischel Avatar answered Jan 07 '23 21:01

Jim Mischel