Change system time using vb.net

Does anyone know how to change the system clock using code. I want to be able to set the date and time in my code.
Any thoughts
Thanks!!!


Answer this question

Change system time using vb.net

  • rbroadbent

    well yes the above does it pretty much except you need to place the code in the right place. So declare a datetime object globally as shown. Then in your plus button, add 1 hour (for example) to the datetime object stored globally:

    Me.theDateTime = Me.theDateTime.AddHours(1)

    for minus button, do the same thing but do -1 instead to take away 1 hour. Same goes for minutes, seconds, milliseconds, days, months and years.



  • Poh Tham

    I dont think you are exactly reading my post.
    I dont just want the time on the form
    I have several buttons. ex. first button has a plus sign, second button has a minus sign. when i click on the button with the plus sign it should increase the hour or minute by 1. It is just as if i was setting the time on the pc clock. I hope this is clear.
    Thanks!!

  • starLisa

    its because you should be doing it this way, reassigning it back to the global variable as shown earlier:

    Me.theCustomDateTime = Me.theCustomDateTime.AddHours(1)

    Me.txthour.Text = Me.theCustomDateTime.ToString()



  • Susan S

    here is what i have
    Global declared

    Public theCustomDateTime As DateTime = New DateTime(Today.Hour)

    i have a button and a textbox

    in the button code i have
    txthour.Text = theCustomDateTime.AddHours(1)

    when i click the button 1 displays in the textbox but when i click it again nothing happens. I want each time i click the button the hour increases by 1 and goes until 23. This should be changing the system clock so that whatever hour i put it as the system clock will change to that hour.

  • tackett

    to get the system time:

    DateTime.Now

    returns back the current time. So you could say, place this on a timer on your form, and set the timer interval to say, 1 second (1000 ms's) then do, on the timer tick event:

    Me.theLabel.Text = DateTime.Now.ToString()

    this will keep the label updated every second of the current time the system has.

    if you create your own DateTime object, again using the approach above, you can then add 1 second to the DateTime object you created in your program. Example:

    Dim theCustomDateTime as DateTime = new DateTime(year, month, day, hour, minute, second) 'declare this globally

    'timer tick event:

    Me.theCustomDateTime = Me.theCustomDateTime.AddSeconds(1)

    Me.theLabel.Text = Me.theCustomDateTime.ToString()

    something more towards your area



  • Robert K S

    This is not doing what i want.
    what I want to do is connect the system time to my program
    i have a interface where the user can go in and click (for example: the + and - button to increase or decrease the number to change the hour and minutes.) I want them to be able to change the time and date as if they were doing it using the windows date and time properties. When they change the time in the program i want the system clock to also change accordingly. I know how write code to set the current date and time by clicking a button. I just need to know how to get the clock in the program with the ability to adjust the date and time.
    Any thoughts
    Thanks!!


  • ginnyK

    I am not sure how to import the user32.dll
    I am not even sure what this is. I'm i declaring something

    <DllImport("user32.dll", setLastError:=True)>public shared function SetSystemTime(byref theDateTime as SYSTEMTIME) as Boolean End Function
    i dont understand this line and where to put it.

  • Tarana

    It is not a third party program. I dont want to just click a button and the date and time is displayed. The program i am using is time orentied. The time in the program keeps up with the system time or whatever time u set it to. I want to be able to edit the time through a form in my program. NOT just click a button and set the current date and time. I want what i stated in my previous post.

  • maverick_majnoo

    which program As you may know, the code above does this, it sets the system date/time according to what ever you have set in the structure SYSTEMTIME. in regards to your program, what program is this Something you have made or some other 3rd party software

    I guess when you implemented say a numericupdown control, this could be, as an example, the day to set. Then you set this value to the structure wDay, and you do the same for the rest of the properties. Once done and they hit the button save, do the code above



  • Yennam

    indeed you are declaring it and thats how you import a dll/Win32 (also known as P/Invoke)

    this goes at the top of the class, just after public class form1 for example, the same applies for the structure decleration. Snippet:


    Public Class Form1
    Structure 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

    <DllImport("kernel32.dll", setLastError:=True)> Public Shared Function SetSystemTime(ByRef theDateTime As SYSTEMTIME) As Boolean

    End Function
     
    private sub button1_Click(byval sender as object, byval e as EventArgs) handles button1.Click
     
    end sub
     
    'and so on


     

     

    edit: change user32 to kernel32



  • sunrunner

    :-) you need to replace the bold words as those are parameter values. In the IDE when you type code and you open the bracket "(", it will give you a tooltip explaining what overloads this class has, those overloads I put in here for you as an example. So if you just wanted to start from todays date/time then:

    Dim theCustomDateTime as DateTime = DateTime.Now

    and thats it! It will create a datetime object with the current time (system time) in a global variable. If you wanted to specify the year/month/day/hour/minute/second etc... then you fill in the appropriate parameter values:

    Dim theCustomDateTime As DateTime = New DateTime(2006, 11, 1, 02, 11, 30)



  • Kamarey

    when i do the global declare of Public theCustomDateTime As DateTime = New DateTime(Year, Month, Day, Hour, Minute, Second) like you suggested it gives me this message.

    Argument not specified for parameter 'DateValue' of 'Public Function year (DateValue as Date) as integer'



  • Cordell Swannack

    indeed there is, try this:

    //create a structure:


    Structure 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

     

     

    import the user32.dll (import the System.Runtime.InteropServices namespace):


    <DllImport("kernel32.dll", setLastError:=True)>public shared function SetSystemTime(byref theDateTime as SYSTEMTIME) as Boolean

    End Function


     

    finally, do the job:

     



    Dim newDateTime as SystemTime()
    newDateTime.wDay = valueHere
    if SetSystemTime(newDateTme) then
       'cool, we set it successfully
    end if

     

    does this help/work

     

    once you create the Strucure instance, simply set the value you want to the date/time you like



  • sarathy

    ok, that works, but why does it also show the date in the box.
    All i want to appear in the box is just the hour of day.

  • Change system time using vb.net