DateTime convert to Date and then back to DateTime in C#

.net, c#, date, datetime, datetimepicker

Solution

if `dateTimeWycenaPortfelaObliczDataOd` is of type `DateTime`, You can use:

dateTimeWycenaPortfelaObliczDataOd.Date

to get the date part only (time will be 00:00:00...). If you want to get the very last tick of the date, you can use:

dateTimeWycenaPortfelaObliczDataOd.Date.AddDays(1).AddTicks(-1)

but you really better work with the next date (.AddDays(1)).

In any case, there is no need to convert to string and back to DateTime.

Problem

I use this to convert DateTime value into Date and then I add 00:00:00 and 23:59:59 to make sure whole day is taken into consideration when counting stuff. I'm pretty sure it's wrong way of doing things. What would be the right way? ``` DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00"); DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59"); ```

Original source