计算时间跨度和未来日期的日期差异

发布于 2024-10-02 08:09:30 字数 817 浏览 4 评论 0原文

我正在尝试根据日期列按以下顺序标记网格中的行(

  • 当距离今天 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

少女净妖师 2024-10-09 08:09:31

.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.

浅浅 2024-10-09 08:09:31

我可能会建议另一种编写代码的方法:

Dim age As Double = Now.Substract(myDate).TotalDays

If age >= 2 Then
  e.Value = ImageCollection2.Images(3) //Red
ElseIf age >= 1 Then
  e.Value = ImageCollection2.Images(1) //Yellow
ElseIf age >= 0 Then
  e.Value = ImageCollection2.Images(0) //Green
Else
  e.Value = ImageCollection2.Images(4) //Blue
End If

正如我最初的评论中提到的,除非您在未来至少 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:

Dim age As Double = Now.Substract(myDate).TotalDays

If age >= 2 Then
  e.Value = ImageCollection2.Images(3) //Red
ElseIf age >= 1 Then
  e.Value = ImageCollection2.Images(1) //Yellow
ElseIf age >= 0 Then
  e.Value = ImageCollection2.Images(0) //Green
Else
  e.Value = ImageCollection2.Images(4) //Blue
End If

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.

陪你到最终 2024-10-09 08:09:31

我找到了解决方案。

我首先使用 Date.Compare 用 IF 包装我的 CASE,以首先检查该日期是否是将来的日期。

If Date.Compare(myDate, Now) < 0 Then
                Select Case Now.Subtract(delivDate).Days

                    Case Is >= 2
                        '2 or more days old then RED FLAG
                        e.Value = ImageCollection2.Images(3)
                    Case 1
                        '1 day old then YELLOW FLAG
                        e.Value = ImageCollection2.Images(1)
                    Case Else
                        '0 day (current day) then GREEN FLAG
                        e.Value = ImageCollection2.Images(0)
                End Select
            Else
                'DATE IS IN THE FUTURE
                e.Value = ImageCollection2.Images(4)
            End If

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.

If Date.Compare(myDate, Now) < 0 Then
                Select Case Now.Subtract(delivDate).Days

                    Case Is >= 2
                        '2 or more days old then RED FLAG
                        e.Value = ImageCollection2.Images(3)
                    Case 1
                        '1 day old then YELLOW FLAG
                        e.Value = ImageCollection2.Images(1)
                    Case Else
                        '0 day (current day) then GREEN FLAG
                        e.Value = ImageCollection2.Images(0)
                End Select
            Else
                'DATE IS IN THE FUTURE
                e.Value = ImageCollection2.Images(4)
            End If
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文