DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Snippets has posted 5883 posts at DZone. View Full User Profile

Set Date & Time

  • submit to reddit
public struct SystemTime
 public ushort Year;
 public ushort Month;
 public ushort DayOfWeek;
 public ushort Day;
 public ushort Hour;
 public ushort Minute;
 public ushort Second;
 public ushort Millisecond;

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

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


public static void Test()
 SystemTime newTime = new SystemTime();
 newTime.Year = (ushort)2005;
 newTime.Month = (ushort)12;
 newTime.Day = (ushort)2;
 newTime.Hour = (ushort)12; //UTC time (if you are in UTC+2 zone then you'll put here: time - 2h)
 newTime.Minute = (ushort)42;
 newTime.Second = (ushort)11;
 Win32SetSystemTime(ref newTime);