40 out of 77 rated this helpful - Rate this topic

SYSTEMTIME structure

Applies to: desktop apps | Metro style apps

Specifies a date and time, using individual members for the month, day, year, weekday, hour, minute, second, and millisecond. The time is either in coordinated universal time (UTC) or local time, depending on the function that is being called.

Syntax

typedef struct _SYSTEMTIME {
  WORD wYear;
  WORD wMonth;
  WORD wDayOfWeek;
  WORD wDay;
  WORD wHour;
  WORD wMinute;
  WORD wSecond;
  WORD wMilliseconds;
} SYSTEMTIME, *PSYSTEMTIME;

Members

wYear

The year. The valid values for this member are 1601 through 30827.

wMonth

The month. This member can be one of the following values.

ValueMeaning
1

January

2

February

3

March

4

April

5

May

6

June

7

July

8

August

9

September

10

October

11

November

12

December

 

wDayOfWeek

The day of the week. This member can be one of the following values.

ValueMeaning
0

Sunday

1

Monday

2

Tuesday

3

Wednesday

4

Thursday

5

Friday

6

Saturday

 

wDay

The day of the month. The valid values for this member are 1 through 31.

wHour

The hour. The valid values for this member are 0 through 23.

wMinute

The minute. The valid values for this member are 0 through 59.

wSecond

The second. The valid values for this member are 0 through 59.

wMilliseconds

The millisecond. The valid values for this member are 0 through 999.

Remarks

It is not recommended that you add and subtract values from the SYSTEMTIME structure to obtain relative times. Instead, you should

The system can periodically refresh the time by synchronizing with a time source. Because the system time can be adjusted either forward or backward, do not compare system time readings to determine elapsed time. Instead, use one of the methods described in Windows Time.

Examples

The following example demonstrates the difference between the time values retrieved by the GetSystemTime and GetLocalTime functions.


#include <windows.h>
#include <stdio.h>

void main()
{
    SYSTEMTIME st, lt;
    
    GetSystemTime(&st);
    GetLocalTime(&lt);
    
    printf("The system time is: %02d:%02d\n", st.wHour, st.wMinute);
    printf(" The local time is: %02d:%02d\n", lt.wHour, lt.wMinute);
}



// Sample output

The system time is: 19:34
 The local time is: 12:34

Requirements

Minimum supported client

Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Header

Winbase.h (include Windows.h)

See also

FILETIME
FileTimeToSystemTime
GetLocalTime
GetSystemTime
ULARGE_INTEGER
SetLocalTime
SetSystemTime
SystemTimeToFileTime

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012

Did you find this helpful?
(1500 characters remaining)
Community Content Add
Annotations FAQ
VB.Net structure
I'm sure you probably mean well, but please don't post code to the community section until you have tested it - it only makes our jobs harder. 

The correct VB.Net syntax is;

    <StructLayout(LayoutKind.Sequential)> 
    Public Structure SYSTEMTIME
        Public Year As Int16
        Public Month As Int16
        Public DayOfWeek As Int16
        Public Day As Int16
        Public Hour As Int16
        Public Minute As Int16
        Public Second As Int16
        Public Milliseconds As Int16
    End Structure

Integers are 32 bit in .Net - you correctly used uShort in C# but an Integer in VB.

To use the Win32 functions, you can import them as follows;
    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Function SetSystemTime(ByRef lpSystemTime As SYSTEMTIME) As Integer
    End Function
    <DllImport("coredll.dll", SetLastError:=True)> _
    Public Shared Sub GetSystemTime(ByRef lpSystemTime As SYSTEMTIME)
    End Sub

To interrogate problems, use;

Dim Win32ErrorCode As Long = Marshal.GetLastWin32Error()
(Marshal is found in the System.Runtime.InteropServices NameSpace)

A list of error codes can be found at; http://msdn.microsoft.com/en-us/library/ms681381(v=VS.85).aspx (Correct as of time of writing, but we all know MS like to move their links around a bit!)
Alternative to calling into unmanaged code

As an alternative to calling this unmanaged API, you might consider using .NET's system.datetime structure. A DateTime value is used to represent a date and/or a times. Supported values range from 12:00:00 midnight, January 1, 0001 Anno Domini (Common Era) through 11:59:59 P.M., December 31, 9999 A.D. (C.E.). The DateTime.Now property gets the current date and time from the system clock.

For more information about this class, see http://msdn.microsoft.com/en-us/library/system.datetime(VS.85).aspx

vb.net syntax
<StructLayout(LayoutKind.Sequential)> _
Public Structure SYSTEMTIME Public Year As Integer
Public Month As Integer
Public DayOfWeek As Integer Public Day As Integer Public Hour As Integer Public Minute As Integer Public Second As Integer Public Milliseconds As Integer End Structure
C# syntax
[StructLayout(LayoutKind.Sequential)]
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 Milliseconds;
}