首页 > 解决方案 > 使用 Visual Basic 在 DateTime 中使用变量

问题描述

我目前有这个完美运行的 If 语句:

If (Today > New DateTime(2020, 2, 14, 0, 0, 0)) And (Today < New DateTime(2020, 2, 18, 0, 0, 0)) Then
        deliveryText = "I'm working"
      Else

但是,现在我需要使用相同的语句,但更改变量的某一天

例如,我从客户那里得到的变量是 X ,它将替换当天。我不知道如何使它工作的正确语法,这就是我在脑海中看到的方式,在这种情况下 X = 17

If (Today > New DateTime(2020, 2, X - 3, 0, 0, 0)) And (Today < New DateTime(2020, 2, X + 1, 0, 0, 0)) Then
        deliveryText = "I'm working"
      Else

是否有意义?有可能吗?

标签: vb.netdatetime

解决方案


如果变量表示与特定日期的偏移量,那么您应该从该特定日期开始,然后偏移量,例如

Dim currentDate = Date.Today
Dim rootDate = #2/01/2020# 'Use a date literal if you know the specific value at design time.

Dim startDate = rootDate.AddDays(x - 2)
Dim endDate = rootDate.AddDays(x)

If currentDate > startDate AndAlso currentDate < endDate Then

如果偏移天数使您超出 中指定的月份,这允许更改月份rootDate


推荐阅读