Set System Date / Time with VB.Net

Posted on 2005-04-27
Last Modified: 2010-03-10
I need to change the system date / time in a program. Who knows a simple way to do this?
Question by:jrscherer
    LVL 9

    Author Comment

    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.
    LVL 9

    Assisted Solution

    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
    LVL 19

    Accepted Solution


    ' 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

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    Article by: jpaulino
    XML Literals are a great way to handle XML files and the community doesn’t use it as much as it should.  An XML Literal is like a String ( Literal, only instead of starting and ending with w…
    If you're writing a .NET application to connect to an Access .mdb database and use pre-existing queries that require parameters, you've come to the right place! Let's say the pre-existing query(qryCust) in Access takes a Date as a parameter and l…
    This video is in connection to the article "The case of a missing mobile phone (". It will help one to understand clearly the steps to track a lost android phone.
    Internet Business Fax to Email Made Easy - With eFax Corporate (, you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…

    745 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    20 Experts available now in Live!

    Get 1:1 Help Now