VB: Change the computer time

Solved
Anonyme209 Posted messages 761 Status Member -  
Anonyme209 Posted messages 761 Status Member -
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

  1. Patrice33740 Posted messages 8400 Registration date   Status Member Last intervention   1 784
     
    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
    0
  2. Anonyme209 Posted messages 761 Status Member 19
     
    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.
    0
  3. michel_m Posted messages 18903 Registration date   Status Contributor Last intervention   3 320
     
    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
    0
  4. Anonyme209 Posted messages 761 Status Member 19
     
     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
    0
    1. Patrice33740 Posted messages 8400 Registration date   Status Member Last intervention   1 784
       
      And with Label1.Caption instead of Label4.Text?
      0
  5. Anonyme209 Posted messages 761 Status Member 19
     
    In VB.NET, there is no 'Caption' property (at least for labels, text boxes...).
    0
  6. Anonyme209 Posted messages 761 Status Member 19
     
    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.
    0
  7. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    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
    0
  8. Anonyme209 Posted messages 761 Status Member 19
     
     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.
    0
  9. cs_Le Pivert Posted messages 8437 Status Contributor 730
     
    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
    0
  10. Anonyme209 Posted messages 761 Status Member 19
     
    Hello,

    the date works, but the time is not changed.

    Today = CDate(Label1.Text)
    0
  11. cs_Le Pivert Posted messages 8437 Status Contributor 730
     


     TimeOfDay = #10:15:00 PM#


    --
    @+ The Woodpecker
    0
  12. Anonyme209 Posted messages 761 Status Member 19
     
    Subject resolved:

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


    Thank you all for your help.
    0