VB: Change the computer time
Solved
Anonyme209
Posted messages
761
Status
Member
-
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:
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.
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
-
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 -
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. -
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 -
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 -
In VB.NET, there is no 'Caption' property (at least for labels, text boxes...).
-
Hello,
Dim newdate As New System.DateTime(2014, 11, 25, 22, 15, 0) Label1.Text = newdate
to consult for dates
https://plasserre.developpez.com/cours/vb-net/?page=langage-vb8#LV-AB-2
--
@+ The Woodpecker -
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.
-
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 -
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. -
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 -
Hello,
the date works, but the time is not changed.
Today = CDate(Label1.Text)
-
TimeOfDay = #10:15:00 PM#
--
@+ The Woodpecker -
Subject resolved:
Today = CDate(MaskedTextBox2.Text) TimeOfDay = CDate(MaskedTextBox2.Text)
Thank you all for your help.