VB: Change the computer time

Solved
Hello,
I am looking for how to change the computer's time in vb.net.

For example, my Label1.Text = "25/04/2014 14:45:59"
I found:
DateString = CDate(Format(CDate(Label1.Text), "dd/MM/yyyy")) TimeString = CDate(Format(CDate(Label1.Text), "HH:mm:ss"))

But I receive an error message stating that it is not a valid date...
(Conversion from string "25/04/2014" to type 'Date' is not valid.)
However, I don't see why it is not a valid date

Thank you for your help.

13 answers

  • Votes
  • Date
  • Descending date
  1. In VBA, this works:
    Private Sub CommandButton1_Click() Dim Datestring As Date Dim Timestring As Date Label1.Caption = "27/04/2014 14:52:02" Datestring = CDate(Format(CDate(Label1.Caption), "dd/MM/yyyy")) Timestring = CDate(Format(CDate(Label1.Caption), "HH:mm:ss")) MsgBox Datestring & " " & Timestring End Sub 


    --
    Best regards
    Patrice
    1. Label1.Text = date and time from the Internet

      Dim Datestring1 As Date Dim Timestring1 As Date Datestring1 = CDate(Format(CDate(Label1.Text), "dd/MM/yyyy")) Timestring1 = CDate(Format(CDate(Label1.Text), "HH:mm:ss")) DateString = Datestring1 TimeString = Timestring1


      Line 5:
      Conversion from string "27/11/2014" to type 'Date' is not valid.
      1. Contributor
        Hello

        You convert to string (format) and revert to date (cdate) 25/4/14 is not a date (English notation) because it is translated in the format m/D:Y so 4/25/14
        --
        Michel
        1.  Dim Datestring1 As Date Dim Timestring1 As Date Datestring1 = CDate(Format(CDate(Label4.Text), "M/d/y")) Timestring1 = CDate(Format(CDate(Label4.Text), "H:m:s")) DateString = Datestring1 TimeString = Timestring1


          Line 3:
          Conversion from string "11/27/14" to type 'Date' is not valid.

           Dim Datestring1 As Date Dim Timestring1 As Date Datestring1 = CDate(Format(CDate(Label4.Text), "M/d/yyyy")) Timestring1 = CDate(Format(CDate(Label4.Text), "H:m:s")) DateString = Datestring1 TimeString = Timestring1


          Line 3:
          Conversion from string "11/27/2014" to type 'Date' is not valid.


          MsgBox(IsDate(Datestring1))

          True
          1. And with Label1.Caption instead of Label4.Text?
        2. In VB.NET, there is no 'Caption' property (at least for labels, text boxes...).
          1. Hello,

            Dim newDate As New System.DateTime(2014, 11, 25, 22, 15, 0) DateString = newDate


            Line 2:
            Conversion from string "25/11/2014 22:15:00" to type 'Date' is not valid.
            1. Contributor
              And like this:

               Dim Datestring As Date Dim nouvelledate As New System.DateTime(2014, 11, 25, 22, 15, 0) Datestring = nouvelledate MsgBox(DateString)


              --
              @+ The Woodpecker
              1.  Dim Datestring As Date Dim nouvelledate As New System.DateTime(2014, 11, 25, 22, 15, 0) Datestring = nouvelledate MsgBox(Datestring)


                The MsgBox returns "25/11/2014 22:15:00"
                but the date and time are not changed.
                1. Contributor
                  Here is your request, I believe I have responded!

                  But an error message saying that it is not a valid date pops up...
                  (Conversion from string "25/04/2014" to type 'Date' is not valid.)
                  However, I don't see what makes it an invalid date


                  but the date and time are not changed.

                  Naturally, I don't see any code that does that!

                  As for changing the date and time of the PC, see this:

                  https://www.vbforums.com/showthread.php?584028-how-to-PROPERLY-change-SetLocaltime

                  'you can use the keyword today:

                  Today = #1/1/2005#

                  'your program will need to have administrator privileges to change the system date

                  --
                  @+ The Woodpecker
                  1. Hello,

                    the date works, but the time is not changed.

                    Today = CDate(Label1.Text)
                    1. Subject resolved:

                      Today = CDate(MaskedTextBox2.Text) TimeOfDay = CDate(MaskedTextBox2.Text)


                      Thank you all for your help.