?
Solved

Set System Date / Time with VB.Net

Posted on 2005-04-27
3
Medium Priority
?
1,661 Views
Last Modified: 2010-03-10
I need to change the system date / time in a vb.net program. Who knows a simple way to do this?
0
Comment
Question by:jrscherer
3 Comments
 
LVL 9

Author Comment

by:jrscherer
ID: 13883011
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
 
LVL 9

Assisted Solution

by:checoo
checoo earned 500 total points
ID: 13883025
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
 
LVL 19

Accepted Solution

by:
Fahad Mukhtar earned 500 total points
ID: 13883133
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

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This tutorial demonstrates one way to create an application that runs without any Forms but still has a GUI presence via an Icon in the System Tray. The magic lies in Inheriting from the ApplicationContext Class and passing that to Application.Ru…
Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
this video summaries big data hadoop online training demo (http://onlineitguru.com/big-data-hadoop-online-training-placement.html) , and covers basics in big data hadoop .
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…

807 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