Get the Time and Date Details of Your System Using WMI Class

Introduction

This article describes how to get the Time and Date details of your system using a WMI Class. Here I will get the information from the Win32_CurrentTime class.

What Win32_CurrentTime is

The Win32_CurrentTime abstract is a singleton WMI class that describes a point in time by using the component items, such as milliseconds, seconds, minutes, hours, days, days of the week, week in the month, months, quarters, and years.

The following two important classes are derived from this class. Win32_LocalTime allows you to monitor time in local reference and Win32_UTCTime allows you to monitor time in coordinated universal time (UTC) reference. 

Design
 
Create a new Windows Forms Application Project.

Add one button control to the form.

Design your screen as in the following screen:

WMI-Class-1.jpg

Next add a reference for "System.Management".

To add the reference use the following procedure.

Go to Solution Explorer, select the project and right-click on that and choose "Add Reference" from the menu.

WMI-Class-2.jpg


A window will open; in that choose the ".Net" tab.

WMI-Class-3.jpg

It will show a list. In that list, choose "System.Management" and click the "OK" Button.

WMI-Class-4.jpg

Now go to code view.

Add the namespace "using System.Management;".

Write the following code in the Button Click event
:

        private
 void button1_Click(object sender, EventArgs e)
        {
            SelectQuery Sq = new SelectQuery("Win32_CurrentTime");
            ManagementObjectSearcher objOSDetails = new ManagementObjectSearcher(Sq);
            ManagementObjectCollection osDetailsCollection = objOSDetails.Get();
            StringBuilder sb = new StringBuilder();
            foreach (ManagementObject mo in osDetailsCollection)
            {
                sb.AppendLine(string.Format("----------------------------"));
                sb.AppendLine(string.Format("Class : {0}", mo["__class"].ToString()));
                sb.AppendLine(string.Format("Day : {0}", mo["Day"].ToString()));
                sb.AppendLine(string.Format("DayOfWeek: {0}", mo["DayOfWeek"].ToString()));
                sb.AppendLine(string.Format("Hour: {0}", mo["Hour"].ToString()));
                sb.AppendLine(string.Format("Milliseconds: {0}", mo["Milliseconds"]).ToString());
                sb.AppendLine(string.Format("Minute: {0}", mo["Minute"].ToString()));
                sb.AppendLine(string.Format("Month : {0}", mo["Month"].ToString()));
                sb.AppendLine(string.Format("Quarter: {0}", mo["Quarter"].ToString()));
                sb.AppendLine(string.Format("Second : {0}", mo["Second"].ToString()));
                sb.AppendLine(string.Format("WeekInMonth: {0}", mo["WeekInMonth"].ToString()));
                sb.AppendLine(string.Format("Year : {0}", mo["Year"].ToString()));
            }
            MessageBox.Show(sb.ToString());
        }
 

In the code above I am getting the information from Win32_CurrentTime and showing it in a Message Box on a button click.

SelectQuery

It represents a WMI Query Language (WQL) SELECT data query.

ManagementObjectSearcher

It retrieves a collection of management objects based on a specified query. 

This class is one of the more commonly used entry points to retrieve management information.

ManagementObjectCollection

It represents various collections of management objects retrieved using WMI.

Now build your application. Click on the button. It will show the
Current time and date (UTC Time and Local Time) details in a Message box.

WMI-Class-5.jpg

Up Next
    Ebook Download
    View all
    Learn
    View all