计算时间跨度和未来日期的日期差异
我正在尝试根据日期列按以下顺序标记网格中的行(
- 当距离今天 2 天或更多天时) 然后是红色
- 当 1 天前然后是黄色
- 当 0 天前然后是绿色
- 当日期是将来的时候 蓝色
我有以下工作正常,除了未来的日期是绿色而不是蓝色。
Dim myDate As DateTime = CType(grdSummaryView.GetRowCellValue(e.RowHandle, "myDate"), DateTime)
Select Case Now.Subtract(myDate).Days
'2 or more days old then RED FLAG
Case Is >= 2
e.Value = ImageCollection2.Images(3)
Case 1
'1 day old then YELLOW FLAG
e.Value = ImageCollection2.Images(1)
Case 0
'Current day then GREEN FLAG
e.Value = ImageCollection2.Images(0)
Case Else
e.Value = ImageCollection2.Images(4)
End Select
I am trying to flag rows in a grid in the following order based on a date column
- When 2 or more days old from today
then RED - When 1 day old then YELLOW
- When 0 days old then GREEN
- When the date is in the future then
BLUE
I have the following which is working fine except for the future dates which are GREEN instead of BLUE.
Dim myDate As DateTime = CType(grdSummaryView.GetRowCellValue(e.RowHandle, "myDate"), DateTime)
Select Case Now.Subtract(myDate).Days
'2 or more days old then RED FLAG
Case Is >= 2
e.Value = ImageCollection2.Images(3)
Case 1
'1 day old then YELLOW FLAG
e.Value = ImageCollection2.Images(1)
Case 0
'Current day then GREEN FLAG
e.Value = ImageCollection2.Images(0)
Case Else
e.Value = ImageCollection2.Images(4)
End Select
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
.Days 始终给出一个 Integer 值,因此只有在未来至少 24 小时后它才会起作用。您可以按照自己的建议解决它,也可以立即处理时间跨度差异。
您可能还需要考虑这种差异的含义。 2 天前是否意味着您要选择 48 小时前创建的元素,或者是否意味着 11 月 10 日创建的所有条目。
The .Days always gives an Integer value so it won't work until you are at least 24 hours into the future. You can either solve it as you suggested yourself or work immediately with the Timespan difference.
You also might want to consider the meaning of this difference. Does 2 days old mean that you want to select elements that were created 48 hours ago or does it mean all entries made on the 10th of November for instance.
我可能会建议另一种编写代码的方法:
正如我最初的评论中提到的,除非您在未来至少 24 小时,否则 Days 将返回 0。因此,如果是 2010/08/15 12:30:00 并且您未来的日期是 2010/08/16 0:30:00 那么 TimeSpan 是 -00:12:00:00 等,天数将为 0。
I might suggest an alternate way of writing the code:
As mentioned in my initial comments, Days will return 0 unless you are at least 24 hours in the future. So if it is 2010/08/15 12:30:00 and your future date is 2010/08/16 0:30:00 then the TimeSpan is -00:12:00:00 etc and Days will be 0.
我找到了解决方案。
我首先使用 Date.Compare 用 IF 包装我的 CASE,以首先检查该日期是否是将来的日期。
I found a solution.
I first wrap my CASE with a IF using Date.Compare to first check if the date is in the future.