Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get the maximum time of a date

Tags:

c#

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

like image 777
Almaji Avatar asked Nov 29 '11 23:11

Almaji


1 Answers

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

Update As per comments by @RenatoGama on the question:

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

Sample:

'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'

like image 170
Meligy Avatar answered Sep 17 '22 13:09

Meligy