简体   繁体   English

将DateTime设置为11:59:59 PM

[英]Set DateTime to 11:59:59 PM

We have an app which let's users get data between two dates. 我们有一个应用程序,可让用户获取两个日期之间的数据。 These dates are in a Date control and if the user wants to select data for one day they would enter in the same day in both controls. 这些日期位于“日期”控件中,如果用户要选择某一天的数据,则可以在同一天在两个控件中输入。

This will cause an issue because the .Value of these controls is set to midnight, 12:00 AM . 这将引起问题,因为这些控件的.Value设置为午夜12:00 AM So it will attempt to look for data between 12:00 AM and 12:00 AM of the same day. 因此它将尝试在同一天的12:00 AM12:00 AM之间查找数据。

For example, 例如,

DateTime start = fromDate.Value; // 09-07-2018 12:00 AM
DateTime end = toDate.Value; // 09-07-2018 12:00 AM

Later on, we will call the database where start is :start and end is :end . 稍后,我们将调用start:startend:end

SELECT * FROM Table WHERE Date BETWEEN :start AND :end;

This query will return nothing as nothing is between midnight and midnight. 该查询将不返回任何内容,因为在午夜和午夜之间什么都没有。

How do we set the end to 23:59:59 or 11:59:59 PM ? 我们如何将end时间设置为23:59:5911:59:59 PM

You should just use 你应该用

DateTime end = toDate.Value.AddDays(1);

This will give you midnight of the next day, so anything less than this is the prior day. 这将给您第二天的午夜,因此,比前一天少的多。

您需要使用下一个代码:

DateTime end = toDate.Value.AddDays(1).AddSeconds(-1);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何转换日期时间格式为23:59:59 - how to convert datetime in format 23:59:59 连接时 SqlDateTime 溢出。 必须在 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间 - SqlDateTime overflow on join. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM 错误 - SqlDateTime 溢出。 必须在 1/1/1753 12:00:00 AM 和 12/31/9999 11:59:59 PM 之间 - Error - SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM SqlDateTime溢出。 必须介于1/1/1753 12:00:00 AM和12/31/9999 11:59:59 PM之间 - SqlDateTime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM 更改 DateFormatString 以显示 11:59:59 而不是 12:00:00 - Change DateFormatString to show 11:59:59 as opposed to 12:00:00 System.data.sqltypes.sqltypeexception: 'sqldatetime 溢出。 必须在 1753 年 1 月 1 日上午 12:00:00 到 9999 年 12 月 31 日晚上 11:59:59 之间。 - System.data.sqltypes.sqltypeexception: 'sqldatetime overflow. Must be between 1/1/1753 12:00:00 AM and 12/31/9999 11:59:59 PM.' 有效地将大于59(秒)的值转换为DateTime - Converting a value of more than 59 (seconds) effectively into a DateTime DateTime Parse提取ss大于59时的秒数 - DateTime ParseExtract seconds when ss is more than 59 C#TimeSpan超过23:59:59 - C# TimeSpan exceeds 23:59:59 如何获得从周五 23:59:59 开始到周日 23:59:59 结束的周末 - How to get weekend that starting in Friday 23:59:59 and ends in Sunday 23:59:59
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM