[英]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 AM
和12: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
为:start
和end
为: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:59
或11: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.