VB.NET - Expirate Program

Sergio Siqueira 41 Reputation points
2021-09-02T17:01:22.06+00:00

Hello,

I would like to put an expiration date on my program, so that it doesn't run when it reaches the stipulated date. I'm using code below, and it works fine when I run on computers with "dd/mm/yyyy" date format, but when I run on computers with "mm/dd/yyy" date format, the condition doesn't work, and the program is executed. How can I use a condition that works on both date formats?

Dim dt1 As DateTime = DateTime.Now
Dim dt2 As DateTime = DateTime.Parse("31/12/2021")

        If dt1 >= dt2 Then
            MsgBox("Expired - Please Contact the Administrator")
            Application.Exit()

        End If
Developer technologies VB
0 comments No comments
{count} votes

Accepted answer
  1. Xingyu Zhao-MSFT 5,381 Reputation points
    2021-09-03T03:02:53.06+00:00

    Hi @Sergio Siqueira ,
    Use the following code to convert the date to "dd/MM/yyyy" date format.

    Dim dt2 = DateTime.ParseExact("31/12/2021", "dd/MM/yyyy", Nothing)  
    

    Convert to "MM/dd/yyyy" format.

    dt2.ToString("MM/dd/yyyy")  
    

    Hope it could be helpful.

    Best Regards,
    Xingyu Zhao
    *
    If the answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


2 additional answers

Sort by: Most helpful
  1. Castorix31 90,521 Reputation points
    2021-09-02T17:12:12.617+00:00

    You can use DateTime.ParseExact
    (but if the user can change the System date, your test won't work...)


  2. Sergio Siqueira 41 Reputation points
    2021-09-03T02:16:41.397+00:00

    Hi,
    I found out, now it's working

    Than you a lot

    Dim provider As New CultureInfo("en-US")
            Dim dt1 As Date = System.DateTime.Now
            Dim dt2 As DateTime = DateTime.ParseExact("31-12-2020", "mm-dd-yyyy", provider)
    
            If dt1 >= dt2 Then
                MsgBox("Expirou a Validade - Favor Contactar SERGIORS@")
                Me.Close()
    
            End If
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.