比较2个日期,得出3个结果。

时间:2014-04-23 08:10:07

标签: asp.net vb.net compare

我需要将ASP.NET选项卡容器中的headertext图像更改为不同的颜色。例如,今天是23/04/2014和我的标签ImageUrl="~/icons/vwicn114.gif"(使用ID" FireTraffic"),我需要在2天内更新我的SQLserver中的记录那时候,我需要改变ImageURL。所以把它想象成一个TrafficLight系统。这里有一段代码可以帮助理解:

        connection.Open()
    command = New SqlCommand("Select TOP 1 [Due Date] From FireTest Order By [Due Date] Desc", connection)
    Dim DueDate As String = command.ExecuteScalar()

    Dim Mycommand As New SqlCommand("Select TOP 1 [Date] From FireTest Order By [Date] Desc", connection)
    Mycommand = command.ExecuteScalar()

        connection.Close() 

所以我的代码将是:

  If mycommand "is 1 day more than" DueDate then firetraffic.ImageURL=Red  
  If mycommand "is 3 days or less than" DueDate then firetraffic.ImageURL=Yellow
    End If 
   End If

我只是不知道我应该把什么放进显然不是代码的空间,对答案的任何指导都会非常感激。

1 个答案:

答案 0 :(得分:1)

这样的东西? :

Dim d1 As DateTime = DateTime.Now
Dim dueDate As DateTime = .....

Dim result = ""

'If overdue for one day or more'
If (d1 - dueDate).Days >= 1 Then
    result = "red"
'If 3 days or more to due date'
ElseIf (dueDate - d1).Days <= 3 Then
    result = "yellow"
Else : result = "green"
End If