我怎么可以指定日的日期时间的最晚时间时间、最晚、日期、我怎么

2023-09-02 23:49:02 作者:A逼C迪E爱抚鸡

我使用的是的System.DateTime 对象,以允许用户选择一个日期范围。用户只能够选择使用第三方日历,所以我需要自动指定一天的时间就应该使用日期(没有时间):日期后(即00:00:00或23:59:59)被选中。

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 will need to automatically specify the time of day it should use (ie: 00:00:00 or 23:59:59) after the date is chosen.

我如何指定时间之后的日期已经存储的日历选择一个日期对象?我可以使用 AddHours,AddMinutes,AddSeconds 的方法,但这些都是相对于当前时间,这可能不是00:00:00。

How can I specify the time after the date is already stored as a DateTime object by the calendar selector? I could use the AddHours, AddMinutes, AddSeconds methods but those are relative to the current time which may not be 00:00:00.

的startDate 将需要有00:00:00和结束日期一时间有23的时间:59:59至占到整个天

The startDate will need to have a time of 00:00:00 and endDate have a time of 23:59:59 to account for the entire days.

推荐答案

如果您已经创建了一个的DateTime 对象,并要替换的11时59时间: 59PM对于给定的日期,那么你可以使用 .Date 属性来获取日期与时间设置为00:00:00,然后添加的小时,分​​钟和秒。例如:

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);

如果按最新的时间,你的意思是下午11:59: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);
 
精彩推荐