tags:

views:

2720

answers:

5

I need to create a midnight DateTime

I've just done this:

DateTime endTime = DateTime.Now;
endTime.Subtract(endTime.TimeOfDay);

Haven't test it yet, I'm assuming it works but is there a better/cleaner way?

+20  A: 

just use foo.Date, or DateTime.Today for today's date

Marc Gravell
Thanks, I knew what I'd done was a nasty hack!
endian
+4  A: 

DateTime.Today

mmiika
+1  A: 

DateTime.Now.AddDays(1).Date

WebDude
+1  A: 
DateTime endTime = DateTime.Now.Date;

Now endTime.TimeOfDay.ToString() returns "00:00:00"

zendar
A: 

Thanks for this post, it helped me to put this together where I need midnight tonight to be the cut-off point for a database query:

public string MidnightTonight()

        {
            DateTime today = DateTime.Now.Date;
            return today.ToString("yyyy'/'MM'/'dd") + " 23:59:59";
        }

I tried AddDays(1) but if I run the query at 3pm today, that would give me 3pm tomorrow rather than midnight tonight, wouldn't it?

simonk