VB.net创建日期开关(比较两个日期)

VB.net创建日期开关(比较两个日期),vb.net,if-statement,Vb.net,If Statement,我想在我的应用程序中实现一个基于日期的killswitch,以便它在某个时间停止工作。因此,我在日期(如2019年2月1日)算出。一旦系统的日期(我的应用程序运行的地方)大于Killswitch日期,我希望它停止工作。我试过: If Today.Date.toString() >= "02/01/2019" Then End Else ... 那没用。我遇到的问题是,每台计算机上的日期格式不同,有些是MM/dd/YYYY,有些是dd/MM/YYYY,有些是YYYY/MM/dd。是否仍然可

我想在我的应用程序中实现一个基于日期的killswitch,以便它在某个时间停止工作。因此,我在日期(如2019年2月1日)算出。一旦系统的日期(我的应用程序运行的地方)大于Killswitch日期,我希望它停止工作。我试过:

If Today.Date.toString() >= "02/01/2019" Then
End
Else
...

那没用。我遇到的问题是,每台计算机上的日期格式不同,有些是MM/dd/YYYY,有些是dd/MM/YYYY,有些是YYYY/MM/dd。是否仍然可以将所有日期转换为通用格式并进行比较?

如果所有日期都是标准格式,则可以使用DateTime.Parse。此时,您将比较两个日期时间值,然后大于或等于将起作用

If DateTime.Now >= DateTime.Parse("02/01/2019") Then
    Environment.End
End If
为了手动解析日期,您需要知道它的格式,因为您需要从它的片段创建DateTime对象。下面是一个例子:

Dim strDate As String = "12/28/2018"
Dim year As Integer = Integer.Parse(strDate.Substring(6, 4))
Dim month As Integer = Integer.Parse(strDate.Substring(0, 2))
Dim day As Integer = Integer.Parse(strDate.Substring(3, 2))
Dim d = New DateTime(year, month, day)

如果所有日期都是标准格式,则可以使用DateTime.Parse。此时,您将比较两个日期时间值,然后大于或等于将起作用

If DateTime.Now >= DateTime.Parse("02/01/2019") Then
    Environment.End
End If
为了手动解析日期,您需要知道它的格式,因为您需要从它的片段创建DateTime对象。下面是一个例子:

Dim strDate As String = "12/28/2018"
Dim year As Integer = Integer.Parse(strDate.Substring(6, 4))
Dim month As Integer = Integer.Parse(strDate.Substring(0, 2))
Dim day As Integer = Integer.Parse(strDate.Substring(3, 2))
Dim d = New DateTime(year, month, day)

不要硬编码你的约会;那么格式就不成问题了。相反,使用类的构造函数创建具有特定年、月和日的杀死目标:

Dim targetDT As New DateTime(2019, 2, 1)
If DateTime.Today > targetDT Then
    ' ... do something in here ...
End If

不要硬编码你的约会;那么格式就不成问题了。相反,使用类的构造函数创建具有特定年、月和日的杀死目标:

Dim targetDT As New DateTime(2019, 2, 1)
If DateTime.Today > targetDT Then
    ' ... do something in here ...
End If
你可以用这个

    Dim currentDate As DateTime = DateTime.Now
    If currentDate.Month >= 2 And currentDate.Day >= 1 And currentDate.Year >= 2019
          End
    End If
你可以用这个

    Dim currentDate As DateTime = DateTime.Now
    If currentDate.Month >= 2 And currentDate.Day >= 1 And currentDate.Year >= 2019
          End
    End If

另一种方法是,在线评论和解释

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If DateTime.Now > ExpireDate Then
        MessageBox.Show("Your trial period has expired")
        Close()
    End If
End Sub

Private ExpireDate As Date

Private Sub OPCode3()
    'run once at first startup
    'Store As DateTime, then you can compare DateTime withour converserion or parsing
    ExpireDate = DateTime.Now.AddMonths(3)
End Sub

另一种方法是,在线评论和解释

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    If DateTime.Now > ExpireDate Then
        MessageBox.Show("Your trial period has expired")
        Close()
    End If
End Sub

Private ExpireDate As Date

Private Sub OPCode3()
    'run once at first startup
    'Store As DateTime, then you can compare DateTime withour converserion or parsing
    ExpireDate = DateTime.Now.AddMonths(3)
End Sub

注意,他们仍然需要知道它的格式。添加了一个解析指定格式之一的示例。注意,他们仍然需要知道该格式是什么。添加了一个解析指定格式之一的示例。