Over a million developers have joined DZone.

Set Date & Time

DZone's Guide to

Set Date & Time

· ·
Free Resource

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);

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}