Set System Date / Time with VB.Net

I need to change the system date / time in a program. Who knows a simple way to do this?
Who is Participating?
Fahad MukhtarDistinguished EngineerCommented:

' At the very top of your form, (i.e. above the line which reads "Public Class <FormName>"
 add this code:

Imports System.Runtime.InteropServices
Public Structure SYSTEMTIME
    Public wYear As UInt16
    Public wMonth As UInt16
    Public wDayOfWeek As UInt16
    Public wDay As UInt16
    Public wHour As UInt16
    Public wMinute As UInt16
    Public wSecond As UInt16
    Public wMilliseconds As UInt16
End Structure

' Add this code immediately below the #Region " Windows Form Designer generated code " block

 <DllImport("kernel32.dll")> _
  Public Shared Sub GetSystemTime(ByRef lpSystemTime As SYSTEMTIME)
    End Sub

    <DllImport("kernel32.dll")> _
    Public Shared Function SetSystemTime(ByRef lpSystemTime As SYSTEMTIME) As UInt32
    End Function
    Dim systime As New SYSTEMTIME

  Add this function somewhere in the body of the Form Class:

Private Function SetNewTime(ByVal hour As String, ByVal minute As String) As Boolean
        GetSystemTime(systime) ' Optionally, get the current time
        systime.wHour = systime.wHour.Parse(hour) ' set the hour
        systime.wMinute = systime.wMinute.Parse(minute) ' set the minute
' Note that this example will only change the Hour and Minute elements.
' Expand it as needed with additional parameters.
    End Function

Call the function with code similar to this:

 ' Change the system time.(Hour and Minute)
        SetNewTime("19", "26")

To test the code, use syntax similar to:

  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim stime As New SYSTEMTIME
        MessageBox.Show("Time now : " & stime.wHour.ToString & ":" & stime.wMinute.ToString)
        ' Change the system time.(Hour and Minute)
        SetNewTime("12", "59")
        MessageBox.Show("Time now : " & stime.wHour.ToString & ":" & stime.wMinute.ToString)
    End Sub
jrschererCEOAuthor Commented:
Here is a way I found through trial.

            Microsoft.VisualBasic.DateAndTime.Today = WorkingTime
            Microsoft.VisualBasic.DateAndTime.TimeOfDay = WorkingTime

WorkingTime is my new Date. Need to update Date / Time in two different statements. It seems to work.

The tooltip to Today and TimeOfDay say Get only, The Help (F1) says Get and Set.
you have to use P/Invoke

Private Structure <StructLayout(LayoutKind.Sequential)> SYSTEMTIME
Public wYear As Short
Public wMonth As Short
Public wDayOfWeek As Short
Public wDay As Short
Public wHour As Short
Public wMinute As Short
Public wSecond As Short
Public wMilliseconds As Short
End Structure

Private Declare Function SetSystemTime Lib "kernel32" (ByRef lpSystemTime As SYSTEMTIME) As Boolean

Dim lpSystemTime As SYSTEMTIME
' Fill the structure and call
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.