{"id":70563,"date":"2005-01-27T19:57:00","date_gmt":"2005-01-27T19:57:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/01\/27\/how-can-i-determine-the-system-time-on-a-computer\/"},"modified":"2005-01-27T19:57:00","modified_gmt":"2005-01-27T19:57:00","slug":"how-can-i-determine-the-system-time-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-the-system-time-on-a-computer\/","title":{"rendered":"How Can I Determine the System Time on a Computer?"},"content":{"rendered":"<p><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" border=\"0\" alt=\"Hey, Scripting Guy! Question\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" height=\"34\"> \n<P>Hey, Scripting Guy! Is there any way to retrieve the system time as configured on a remote computer?<BR><BR>&#8212; JJ<\/P><IMG border=\"0\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" height=\"5\"><IMG class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" border=\"0\" alt=\"Hey, Scripting Guy! Answer\" align=\"left\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" height=\"34\"><A href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><IMG class=\"farGraphic\" title=\"Script Center\" border=\"0\" alt=\"Script Center\" align=\"right\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" height=\"288\"><\/A> \n<P>Hey, JJ. There are at least two ways to do this, both using WMI. If you\u2019re running Windows XP or Windows Server 2003, you can use the WMI class <B>Win32_LocalTime<\/B>. As you can see, that makes it pretty easy to get the system time: you simply connect to the remote computer, query the Win32_LocalTime class, and then echo the desired time values. In other words:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_LocalTime&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Wscript.Echo &#8220;Month: &#8221; &amp; objItem.Month\n    Wscript.Echo &#8220;Day: &#8221; &amp; objItem.Day\n    Wscript.Echo &#8220;Year: &#8221; &amp; objItem.Year\n    Wscript.Echo &#8220;Hour: &#8221; &amp; objItem.Hour\n    Wscript.Echo &#8220;Minute: &#8221; &amp; objItem.Minute\n    Wscript.Echo &#8220;Second: &#8221; &amp; objItem.Second\nNext\n<\/PRE>\n<P>If you want to get fancy, you can combine all these separate values into a standard date-time string, something that looks like this: <B>1\/26\/2005 8:47:04 AM<\/B>. Here\u2019s a script that can do just that:<\/P><PRE class=\"codeSample\">On Error Resume Next<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_LocalTime&#8221;)<\/p>\n<p>For Each objItem in colItems\n    intMonth = objItem.Month\n    intDay = objItem.Day\n    intYear = objItem.Year<\/p>\n<p>    dtmDate = intMonth &amp; &#8220;\/&#8221; &amp; intDay &amp; &#8220;\/&#8221; &amp; intYear<\/p>\n<p>    intHour = objItem.Hour\n    If intHour &lt; 12 Then \n        strAMPM = &#8220;AM&#8221;\n    Else\n        intHour = intHour &#8211; 12\n        strAMPM = &#8220;PM&#8221;\n    End If<\/p>\n<p>    intMinutes = objItem.Minute\n    If intMinutes &lt; 10 Then\n        intMinutes = &#8220;0&#8221; &amp; intMinutes\n    End If<\/p>\n<p>    intSeconds = objItem.Second\n    If intSeconds &lt; 10 Then\n        intSeconds = &#8220;0&#8221; &amp; intSeconds\n    End If<\/p>\n<p>    dtmTime = intHour &amp; &#8220;:&#8221; &amp; intMinutes &amp; &#8220;:&#8221; &amp; intSeconds &amp; &#8221; &#8221; &amp; strAMPM<\/p>\n<p>    Wscript.Echo dtmDate &amp; &#8221; &#8221; &amp; dtmTime\nNext\n<\/PRE>\n<P>We won\u2019t explain each line of code in the script, but for the most part all we\u2019re doing is combining values. In fact, we only do two things even slightly tricky. First we check the hour to see if it\u2019s less than 12; if it is, we add AM to the end of the string; if it\u2019s not, we add a PM to the end of the string. And, yes, this means that the hour is returned in 24-hour format; thus 3:00 in the afternoon comes back as 15:00. That\u2019s why, when checking the hour, we also subtract 12 if the hour is greater than 12. That way, 15:00 gets converted to 3:00 PM.<\/P>\n<P>For both the minutes and seconds, we check to see if the returned value is less than 10. Why? Well suppose we have 8 hours, 6 minutes, and 5 seconds. If we combine these values as-is, our time will be <B>8:6:5<\/B>. That doesn\u2019t really look like a time, does it? Therefore, if the minutes or the seconds are less than 10, we add a leading zero. When we do that, we\u2019re now combining 8, 06, and 05, and we get a time that looks like this: <B>08:06:05<\/B>. Much better.<\/P>\n<P>Of course, that\u2019s all well and good as long as you\u2019re using Windows XP or Windows Server 2003. But what about people using Windows 2000? Well, that\u2019s OK; you can still get this same information, just not in as straightforward a fashion.<\/P>\n<P>As we noted earlier, the Win32_LocalTime class doesn\u2019t exist on Windows 2000. However, you can still get the system time by querying the <B>Win32_OperatingSystem<\/B> class and checking the value of the <B>LocalDateTime<\/B> property. The only problem with this approach is that the date and time come back in UTC (Universal Time Coordinate) format. Consequently, you get a date and time that looks like this: <B>20050127080605.000000+480<\/B>. Yuck.<\/P>\n<P>We won\u2019t go into the details surrounding the UTC format; if you\u2019re interested you might want to check out <A href=\"http:\/\/null\/technet\/scriptcenter\/guide\/sas_wmi_fvwp.mspx\" target=\"_blank\"><B>this section<\/B><\/A> of the <I>Microsoft Windows 2000 Scripting Guide<\/I>. However, we will show you a script that grabs the LocalDateTime value and converts it to a slightly-more readable format:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)\nSet colItems = objWMIService.ExecQuery(&#8220;Select * from Win32_OperatingSystem&#8221;)<\/p>\n<p>For Each objItem in colItems\n    dtmLocalTime = objItem.LocalDateTime\n    dtmMonth = Mid(dtmLocalTime, 5, 2)\n    dtmDay = Mid(dtmLocalTime, 7, 2)\n    dtmYear = Left(dtmLocalTime, 4)\n    dtmHour = Mid(dtmLocalTime, 9, 2)\n    dtmMinutes = Mid(dtmLocalTime, 11, 2)\n    dtmSeconds = Mid(dtmLocalTime, 13, 2)\nNext<\/p>\n<p>dtmNewDate = dtmMonth &amp; &#8220;\/&#8221; &amp; dtmDay &amp; &#8220;\/&#8221; &amp; dtmYear\ndtmNewTime =  dtmHour &amp; &#8220;:&#8221; &amp; dtmMinutes &amp; &#8220;:&#8221; &amp; dtmSeconds<\/p>\n<p>Wscript.Echo dtmNewDate &amp; &#8221; &#8221; &amp; dtmNewTime\n<\/PRE>\n<P>We haven\u2019t done much in the way of formatting here; for example, we don\u2019t convert 15:00 to 3:00, nor do we indicate whether this is AM or PM. But you can do that yourself, using the same approach we took with the Win32_LocalTime. <\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Is there any way to retrieve the system time as configured on a remote computer?&#8212; JJ Hey, JJ. There are at least two ways to do this, both using WMI. If you\u2019re running Windows XP or Windows Server 2003, you can use the WMI class Win32_LocalTime. As you can see, that makes [&hellip;]<\/p>\n","protected":false},"author":595,"featured_media":87096,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[1],"tags":[13,31,3,4,5,6],"class_list":["post-70563","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-operating-system","tag-scripting-guy","tag-scripting-techniques","tag-vbscript","tag-wmi"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! Is there any way to retrieve the system time as configured on a remote computer?&#8212; JJ Hey, JJ. There are at least two ways to do this, both using WMI. If you\u2019re running Windows XP or Windows Server 2003, you can use the WMI class Win32_LocalTime. As you can see, that makes [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70563","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/users\/595"}],"replies":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/comments?post=70563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/70563\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media\/87096"}],"wp:attachment":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/media?parent=70563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=70563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=70563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}