Set System Date / Time with VB.Net

I need to change the system date / time in a vb.net program. Who knows a simple way to do this?
LVL 9
jrschererCEOAsked:
Who is Participating?
 
Fahad MukhtarDistinguished EngineerCommented:
Refernce:
http://www.vbcity.com/forums/topic.asp?tid=65853

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

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

Code:
 <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:

Code:
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
        SetSystemTime(systime)
' 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:

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



To test the code, use syntax similar to:

Code:
  Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        Dim stime As New SYSTEMTIME
        GetSystemTime(stime)
        MessageBox.Show("Time now : " & stime.wHour.ToString & ":" & stime.wMinute.ToString)
        ' Change the system time.(Hour and Minute)
        SetNewTime("12", "59")
        GetSystemTime(stime)
        MessageBox.Show("Time now : " & stime.wHour.ToString & ":" & stime.wMinute.ToString)
    End Sub
0
 
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.
0
 
checooCommented:
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
SetSystemTime(lpSystemTime)
0
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.