Get maximum time

How can I get the maximum time of a DateTime selected from a datetimepicker? For example, '08 / 21/2011 23:59:59 '

+6
source share
3 answers

myDateTime.AddDays(1).Date.AddSeconds(-1)

Update According to @RenatoGama's comments on the question:

One possible other answer specific to end date scripts is myDateTime.Date.AddDays(1) . which gets you the day before the end date, use < instead of <= (as in dateTocheck < endDate.Date.AddDays(1) ).

Example:

  '08 / 21/2011 13:03:59 '-> .AddDays (1) -> '08 / 22/2011 13:03:59' 

 '08 / 22/2011 13:03:59 '-> .Date -> '08 / 22/2011 00:00:00'  

 '08 / 22/2011 00:00:00 '-> .AddSeconds (-1) -> '08 / 21/2011 23:59:59'
 // OR
 '08 / 22/2011 00:00:00 '-> .AddTicks (-1) -> '08 / 21/2011 23:59:59'
 // Note: A tick is the smallest unit for time difference that DateTime can detect
 // So, technically more accurate answer than using seconds - Thanks @aron

Note: AFAIK, '08 / 22/2011 '== '08 / 22/2011 00:00:00'

+13
source

Meaning of minimun

'08 / 21/2011 00:00:00 '

Maximun value

'08 / 21/2011 23:59:59 '

Application:

  var firstDate = dtpFrom.Value.Date; // 0:00:00 var secondDate = dtpTo.Value.Date.AddSeconds(86400 - 1); //23:59:59 - 86400 is 24 hours var list = Services.GetListForFilter(firstDate, secondDate); 
+1
source
 DateTime d = new DateTime(2017, 1, 13); //d= 13/1/2017 12:00:00 AM d = d.Add(DateTime.MaxValue.TimeOfDay); //d= 13/1/2017 11:59:59 PM 
0
source

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


All Articles