Change System date and time

We Can change our system date and time using following code.

We can get and set the system date and time
using the following code.

namespace Part

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;

structure for passing parameter

public struct MySysDataTime
public ushort MyYear;
public ushort MyMonth;
public ushort MyDay;

public ushort MyHour;
public ushort MyMinute;
public ushort MySecond;

Coding Part

[DllImport("kernel32.dll", EntryPoint = "GetSystemTime", SetLastError = true)]
public extern static void Win32GetSystemTime(ref SystemTime sysTime);

[DllImport("kernel32.dll", EntryPoint = "SetSystemTime", SetLastError = true)]
public extern static bool Win32SetSystemTime(ref SystemTime sysTime);

private void SetSystemDateTime_Click(object sender, EventArgs e)

MySysDataTime NewDataTime = new SystemTime();

NewDataTime.MyYear = (ushort)2010;
NewDataTime.MyMonth = (ushort)4;
NewDataTime.MyDay = (ushort)2;

NewDataTime.MyHour = (ushort)8;
NewDataTime.MyMinute = (ushort)30;
NewDataTime.MySecond = (ushort)5;

Win32SetSystemTime(ref NewDataTime);

MySysDataTime MyCurrentDateTime = new SystemTime();
Win32GetSystemTime(ref MyCurrentDateTime);
MessageBox.Show("This is " + MyCurrentDateTime.MyMonth + " of " + MyCurrentDateTime.MyYear);

Code Explanation
1. Import DLL kerner32.dll
2. Set the entry point GetSystemTime
3. Set the entry point SetSystemTime
4. using Win32SetSystemTime set the system date time
5. using Win32GetSystemTime get the current system date time



No responses found. Be the first to comment...

  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name: