How can I specify the latest time of day with DateTime

后端 未结 12 1077
不思量自难忘°
不思量自难忘° 2020-12-14 14:03

I am using a System.DateTime object to allow a user to select a date range. The user is only able to select a date (not time) using a third party calendar so I

12条回答
  •  执念已碎
    2020-12-14 15:06

    If you already have a DateTime object created and want to replace the time with the 11:59:59PM for that given date, then you can use the .Date property to get the date with time set to 00:00:00 and then add the hours, minutes and seconds. For example:

    var dt = yourDateInstance.Date.AddHours(23).AddMinutes(59).AddSeconds(59);
    

    If by latest time, you mean 11:59:59 PM, then this should also work:

    var dt = new DateTime(Now.Year, Now.Month, Now.Day, 23, 59, 59);
    

提交回复
热议问题