C#: DateTime to Midnight datetime

added by rcash
8/4/2011 8:46:25 PM

741 Views

Here is a bit of code should you ever need to calculate midnight datetime from a specific date. The solution is to subtract the time of day from the date. In the example, midnight datetime is calculated via myDateTime.Subtract(myDateTime.TimeOfDay) i.e. The TimeOfDay property is used here to represent the fraction of the day that has elapsed since midnight.


2 comments

vijayst
8/5/2011 11:31:29 AM
What about DateTime.Today. Even that should work?

dpeterson
8/5/2011 8:33:48 PM
As is pointed out, casting a date to a datetime would also work.
I would not recommend using the example in the blog.