none
VB.NET - Expire the Application - Error: System.FormatException: String was not recognized as a valid DateTime. RRS feed

  • Question

  • Hello,
    I'm trying to put my programs to expire on a date determined by me.
    I am using the code below, but when I run the executable, it gives an error message: System.FormatException: String was not recognized as a valid DateTime.
    If I click "Continue," the program runs normally, and also expires when you give the date to expire.
    but I would like to get this error every time I run, what is wrong with my code?

    Thank you very much in advance.


    'Expiração do programa
            Dim dt1 = DateTime.Now
            Dim dt2 = DateTime.Parse("30/09/2019")

            If dt1 >= dt2 Then
                MsgBox("Expirou a Validade - Favor Contactar o Administrador")
                Application.Exit()
            Else
                Application.Run()
            End If
    Sunday, July 21, 2019 11:39 PM

All replies

  • Hi

    Try this code (the Application.Run() is not valid)

    Dim dt1 As DateTime = DateTime.Now
    Dim dt2 As DateTime = DateTime.Parse("22/09/2019")
    
          If dt1 >= dt2 Then
    MessageBox.Show("Expirou a Validade - Favor Contactar o Administrador")
    	Application.Exit()
          Else
    	'Application.Run()
          End If


    Regards Les, Livingston, Scotland

    Monday, July 22, 2019 12:28 AM
  •  when I run the executable, it gives an error message: System.FormatException: String was not recognized as a valid DateTime.

            Dim dt1 = DateTime.Now
            Dim dt2 = DateTime.Parse("30/09/2019")

    This line:

    Dim dt2 = DateTime.Parse("30/09/2019")

    will cause that exception if your region/localization settings expect dates to 
    be in a different format. You are passing a string that assumes day/month/year
    format. If your version of Windows has the Date setting as month/day/year or
    year/month/day then "30/09/2019" is not a valid date.

    If your Windows settings expect month/day/year date formats then change

    Dim dt2 = DateTime.Parse("30/09/2019")

    to

    Dim dt2 = DateTime.Parse("09/30/2019")

    - Wayne

    Monday, July 22, 2019 2:30 AM
  • Try these variants too:

       Dim dt2 = New DateTime(2019, 9, 30)

    or

       Dim dt2 = DateTime.ParseExact("30/09/2019", "d/M/yyyy", CultureInfo.InvariantCulture)

     

    Monday, July 22, 2019 4:49 AM
  • Note also that you can specify a Culture if you want to use one that may be
    different than what is installed/configured for your Windows. For example.
    even if the date format for the OS is configured for month/day/year you can
    initialize a DateTime using day/month/year by specifying a Culture that
    defaults to that format:

    Dim dt2 = DateTime.Parse("30/09/2019",
              System.Globalization.CultureInfo.GetCultureInfo("fr-Fr"))
    

    - Wayne

    Monday, July 22, 2019 5:31 AM