How to set time to midnight for current day?

.net, c#

Solution

You can use the `Date` property of the DateTime object - eg

DateTime midnight = DateTime.Now.Date;

So your code example becomes

private DateTime _Begin = DateTime.Now.Date;
public DateTime Begin { get { return _Begin; } set { _Begin = value; } }

PS. going back to your original code setting the hours to 12 will give you time of noon for the current day, so instead you could have used 0...

var now = DateTime.Now;
new DateTime(now.Year, now.Month, now.Day, 0, 0, 0);

Problem

Every time that I create a non-nullable datetime in my mvc3 application it defaults to now(), where now is current date with current time. I would like to default it to today's date with 12am as the time. I'm trying to default the time in my mvc...but...the following isn't setting to todays date @12am. Instead it defaults to now with current date and time. ``` private DateTime _Begin = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 12, 0, 0); public DateTime Begin { get { return _Begin; } set { _Begin = value; } } ``` How can I set to 12am for the current date for non-nullable datetime?

Original source