Convert DateTime to Date, then go back to DateTime in C #

I use this to convert the DateTime value to Date, and then add 00:00:00 and 23:59:59 so that the whole day is taken into account when calculating the material. I am pretty sure this is wrong. What will be the right way?

DateTime varObliczOd = DateTime.Parse(dateTimeWycenaPortfelaObliczDataOd.Value.ToShortDateString() + " 00:00:00"); DateTime varObliczDo = DateTime.Parse(dateTimeWycenaPortfelaObliczDataDo.Value.ToShortDateString() + " 23:59:59"); 
+4
source share
8 answers

if dateTimeWycenaPortfelaObliczDataOd is of type DateTime , you can use:

 dateTimeWycenaPortfelaObliczDataOd.Date 

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

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

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

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

+11
source

DateTime objects have a Date property, which may be what you need.

+5
source

You can use the following properties / methods of a DateTime object to get your values:

 DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date; DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataOd.AddDayes(1).AddTicks(-1); 
+2
source

This will help to find out why you need it, but it will work.

 DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Date; DateTime varObliczDo = varObliczOd.AddDays(1).AddSeconds(-1); 

Using the Date attribute, and then directly manipulating it to create the necessary component of time, there is no need to worry about parsing and conversion.

+2
source

You can use the Date property of a DateTime object to accomplish what you need.

 DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.Value.Date; DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1); 

If you really want this to end at 11:59:59 p.m. you can do:

 DateTime varObliczDo = dateTimeWycenaPortfelaObliczDataDo.Value.Date.AddDays(1).AddSeconds(-1); 

Sets varObliczDo as the end date without time plus one day (midnight). Therefore, if dateTimeWycenaPortfelaObliczDataDo was 2010-03-05 16:12:12 , now it will be 2010-03-06 00:00:00 .

+1
source

Something like this, maybe? I typed this out of my head, there are probably some errors in the code.

 DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd.AddSeconds(-dateTimeWycenaPortfelaObliczDataOd.Seconds).AddMinutes(-dateTimeWycenaPortfelaObliczDataOd.Minutes).AddHours(-dateTimeWycenaPortfelaObliczDataOd.Hours); DateTime varObliczDo = new DateTime(dateTimeWycenaPortfelaObliczDataDo.Year, dateTimeWycenaPortfelaObliczDataDo.Month, dateTimeWycenaPortfelaObliczDataDoDay, 23, 59, 59); 
0
source

DateTime newDate = new DateTime (oldDate.Year, oldDate.Month, oldDate.Day, 23, 59.59) DateTime newDate = new DateTime (oldDate.Year, oldDate.Month, oldDate.Day, 0, 0, 0)

0
source

You can work with TimeSpan:

 DateTime varObliczOd = dateTimeWycenaPortfelaObliczDataOd - new TimeSpan(dateTimeWycenaPortfelaObliczDataOd.Hours, dateTimeWycenaPortfelaObliczDataOd.Minutes, dateTimeWycenaPortfelaObliczDataOd.Seconds); 

Similarly, you avoid at least parsing, which may be unsuccessful depending on your local culture settings.

0
source

Source: https://habr.com/ru/post/1303252/


All Articles