vb.net - 使用 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
是否有意义?有可能吗?
解决方案
如果变量表示与特定日期的偏移量,那么您应该从该特定日期开始,然后偏移量,例如
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
。
推荐阅读
- apache-flink - 谈到 Flink 异步 IO 时,“容量”参数是什么?
- single-page-application - oneToMany JPA 春天
- postgresql - 如何在 PostgreSQL Auto Vaccum 中停止分析
- java - 需要优化的 Map(k,v) 例如 (long, long[]),以避免自动装箱
- r - group_by dplyr 返回单值后总结
- python - 当您无法访问启动线程的变量时,如何在 Python 中停止线程?
- typescript - VueJs typescript recursive child components(抛出 [Vue warn]: Unknown custom element)
- python - 使用 numpy 逐像素矢量化 2 个窗口图像集之间的误差计算
- angular - 在 Angular/Karma/Jasmine 中测试 iframe 返回跨站点脚本错误
- assign - 将值分配给框架内小部件的属性