{"id":63513,"date":"2007-11-27T03:59:00","date_gmt":"2007-11-27T03:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/27\/hey-scripting-guy-how-can-i-retrieve-time-zone-information-for-a-computer\/"},"modified":"2007-11-27T03:59:00","modified_gmt":"2007-11-27T03:59:00","slug":"hey-scripting-guy-how-can-i-retrieve-time-zone-information-for-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-retrieve-time-zone-information-for-a-computer\/","title":{"rendered":"Hey, Scripting Guy! How Can I Retrieve Time Zone Information for a Computer?"},"content":{"rendered":"<p><H2><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\"> <\/H2>\n<P>Hey, Scripting Guy! How can I retrieve time zone information for a computer?<BR><BR>&#8212; AL<\/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, AL. You know, over the Thanksgiving Day weekend the Scripting Guy who writes this column took some time to think about his job, and to think of ways in which he could do that job even better. (Note to the Scripting Editor: Believe it or not, the Scripting Guy who writes this column isn\u2019t perfect. He does have <I>some<\/I> room for improvement.) One of the conclusions the Scripting Guy who writes this column came to is this: a daily scripting column should be about scripting. A daily scripting column shouldn\u2019t be about a Scripting Guy <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/aug06\/hey0831.mspx\"><B>riding an exercise bike<\/B><\/A> or ranting about people who <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/may07\/hey0522.mspx\"><B>abuse the express lane<\/B><\/A> at the grocery store. No, a daily scripting column should be about scripting, period. Therefore, no more personal anecdotes; from now on, we\u2019re all business all the time.<\/P>\n<P>You say you have a question first? You\u2019d like to know who won the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov07\/hey1121.mspx\"><B>2007 Turducken Bowl<\/B><\/A>? Let\u2019s put it this way: it was the 2007 Turducken Bowl that caused the Scripting Guy who writes this column to swear off personal anecdotes forever.<\/P>\n<TABLE id=\"ETD\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. Although we might point out that the Scripting Guy who writes this column <I>did<\/I> score all seven of his team\u2019s touchdowns. That includes the touchdown he scored when he took the opening kickoff, faked a lateral to the Scripting Significant Other, and then ran right between the two flabbergasted defenders, giving his team an early 7-0 lead.<\/P>\n<P>And no, that\u2019s not a personal anecdote. That\u2019s just a statement of historical fact.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Besides, you know what they say: it\u2019s not whether you win or lose, it\u2019s how you play the game. And, unfortunately, the Scripting Guy who writes this column and his teammate didn\u2019t play the game very well this year.<\/P>\n<P>But that\u2019s OK; after all, this is a daily scripting column, it\u2019s not a column where we point out that the Scripting Guy who writes the column scored <I>all seven<\/I> of his team\u2019s touchdowns. (Meaning that that his teammate must have scored \u2013 well, we\u2019ll let you do the math on that one.) Likewise, this isn\u2019t a column where we point out that the Scripting Son and Scripting Nephew were guilty of offensive pass interference on pretty much every single play; heaven forbid that we should even <I>suggest<\/I> such a thing. Instead, this is a daily scripting column, a column where we show you how to do scripting type things. You know, things like retrieve time zone information for a computer:<\/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_TimeZone&#8221;)<\/p>\n<p>For Each objItem in colItems\n    Set colItems2 = objWMIService.ExecQuery(&#8220;Select * From Win32_ComputerSystem&#8221;)<\/p>\n<p>    For Each objItem2 in colItems2\n        blnDaylightInEffect = objItem2.DaylightInEffect\n    Next<\/p>\n<p>    If blnDaylightInEffect Then\n        Wscript.Echo objItem.DaylightName\n    Else\n        Wscript.Echo objItem.StandardName\n    End If\nNext\n<\/PRE>\n<P>As you can see, there really isn\u2019t much to this script. We start out by conencting to the WMI service on the local computer, then use the following line of code to return a collection of all the time zones that this computer belongs to:<\/P><PRE class=\"codeSample\">Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_TimeZone&#8221;)\n<\/PRE>\n<P>And before you ask, the answer to both of your questions is this: yes. Yes, you can run this script against a remote computer; all you have to do is assign the name of that computer to the variable strComputer, like so:<\/P><PRE class=\"codeSample\">strComputer = &#8220;atl-fs-01&#8221;\n<\/PRE>\n<P>And yes, our collection will consist of just one item; that\u2019s because, barring any new discoveries in physics, a computer can only be in any one place \u2013 and any one time zone \u2013 at a time.<\/P>\n<TABLE id=\"ERE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Note<\/B>. Wouldn\u2019t it have been nice if the Scripting Guy who writes this column could have been in more than one place at a time, enabling him to, say, both play quarterback <I>and<\/I> wide receiver at the same time? Well, now that you mention it \u2026.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Speaking of time zones, as you probably know, much of the world has a tendency to change times twice a year; in locations like that, part of the year takes place under Daylight Saving Time, and part of the year takes place under Standard Time. (Judging from the view outside the window right now, those of us in Redmond should have done a better job of actually <I>saving<\/I> some daylight during Daylight Saving Time.) Maybe that doesn\u2019t matter to you; maybe all you care about is the name of the time zone itself. If that\u2019s the case, then the For Each loop we\u2019re about to set up (the one that loops through our collection of time zones) can be as simple as this:<\/P><PRE class=\"codeSample\">For Each objItem in colItems\n    Wscript.Echo objItem.Caption\nNext\n<\/PRE>\n<P>What does that give us? That gives us output that looks like this:<\/P><PRE class=\"codeSample\">(GMT-08:00) Pacific Time (US &amp; Canada)\n<\/PRE>\n<P>That\u2019s good: it tells us the time zone (Pacific Time) that the computer is running under. What it <I>doesn\u2019t<\/I> tell us, however, is whether the computer is currently running under Standard Time or Daylight Saving Time. So what the heck; let\u2019s see if we can figure that out as well.<\/P>\n<P>After all, that\u2019s the sort of thing you do in a daily scripting column, right?<\/P>\n<P>With that in mind, the first thing we do within the For Each loop is execute this line of code:<\/P><PRE class=\"codeSample\">Set colItems2 = objWMIService.ExecQuery(&#8220;Select * From Win32_ComputerSystem&#8221;)\n<\/PRE>\n<P>If you\u2019re looking at that and thinking, \u201cWow, that looks like we\u2019re running another WMI query,\u201d well, there\u2019s a good reason for that: we <I>are<\/I> running another WMI query. Although the <B>Win32_TimeZone<\/B> class tells us a considerable amount about a computer and its time zone (more on that in a minute) what it doesn\u2019t tell us is whether that computer is running under Daylight Saving Time or Standard Time. To get <I>that<\/I> information we need to query the <B>Win32_ComputerSystem<\/B> class. Which, needless to say, is exactly what we did.<\/P>\n<P>After executing our second query we next need to set up a second For Each loop, this one designed to loop through the collection of data returned by the Win32_ComputerSystem class. (Again, this will be a collection containing just one item.) Inside this second loop, we use the following line of code to assign the value of the <B>DaylightInEffect<\/B> property to a variable named blnDaylightInEffect:<\/P><PRE class=\"codeSample\">blnDaylightInEffect = objItem2.DaylightInEffect\n<\/PRE>\n<P>What\u2019s the purpose of that? Well, DaylightInEffect is a Boolean value that tells us whether or not the computer is running under Daylight Saving Time. If DaylightInEffect is True that means that the computer <I>is<\/I> running under Daylight Saving Time; if DaylightInEffect is False then that means the computer is running under Standard Time. That also means that all we have to do is examine the value of blnDaylightInEffect and echo back the appropriate message:<\/P><PRE class=\"codeSample\">If blnDaylightInEffect Then\n    Wscript.Echo objItem.DaylightName\nElse\n    Wscript.Echo objItem.StandardName\nEnd If\n<\/PRE>\n<P>As you probably figured out for yourself, <B>DaylightName<\/B> not only gives us the name of the time zone but also tells us that the computer us running under Daylight Saving Time; <B>StandardName<\/B> \u2013 well, you can <I>definitely<\/I> figure out for yourself what StandardName does. When we ran this script in Remdond, WA on November 26, 2007 we got back the following:<\/P><PRE class=\"codeSample\">Pacific Standard Time\n<\/PRE>\n<P>Why? Because our computer is running under Pacific Standard Time, that\u2019s why. If we ran this script a month ago, back when Daylight Saving Time was still in effect, we would have gotten this message instead:<\/P><PRE class=\"codeSample\">Pacific Daylight Time\n<\/PRE>\n<P>Like we said, that information could be extremely useful, especially for enterprises who have computers in both Washington state (where we observe Daylight Saving Time) and in Arizona (where they <I>don\u2019t<\/I> observe Daylight Saving Time.)<\/P>\n<P>But wait: we have a bonus script for you as well. (Yet another thing you\u2019d expect to find in a daily scripting column.) As we noted earlier, the Win32_TimeZone class gives you all sorts of information about a computer and its time zone. What <I>kind<\/I> of information? Well, for one thing, Win32_TimeZone can tell you the exact date when you need to spring ahead (switch to Daylight Saving Time) or fall back (switch to Standard Time). In fact, here\u2019s a script that tells you that very thing:<\/P><PRE class=\"codeSample\">strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colItems = objWMIService.ExecQuery(&#8220;Select * From Win32_TimeZone&#8221;)<\/p>\n<p>For Each objItem in colItems\n    intMonth =  objItem.DaylightMonth\n    Select Case intMonth\n        Case 1 strMonth = &#8220;January&#8221;\n        Case 2 strMonth = &#8220;February&#8221;\n        Case 3 strMonth = &#8220;March&#8221;\n        Case 4 strMonth = &#8220;April&#8221;\n        Case 5 strMonth = &#8220;May&#8221;\n        Case 6 strMonth = &#8220;June&#8221;\n        Case 7 strMonth = &#8220;July&#8221;\n        Case 8 strMonth = &#8220;August&#8221;\n        Case 9 strMonth = &#8220;September&#8221;\n        Case 10 strMonth = &#8220;October&#8221;\n        Case 11 strMonth = &#8220;November&#8221;\n        Case 12 strMonth = &#8220;December&#8221;\n    End Select\n    intDayOfWeek = objItem.DayLightDayOfWeek\n    Select Case intDayOfWeek\n        Case 0 strDayOfWeek = &#8220;Sunday&#8221;\n        Case 1 strDayOfWeek = &#8220;Monday&#8221;\n        Case 2 strDayOfWeek = &#8220;Tuesday&#8221;\n        Case 3 strDayOfWeek = &#8220;Wednesday&#8221;\n        Case 4 strDayOfWeek = &#8220;Thursday&#8221;\n        Case 5 strDayOfWeek = &#8220;Friday&#8221;\n        Case 6 strDayOfWeek = &#8220;Saturday&#8221;\n    End Select\n    intDay = objItem.DaylightDay\n    Select Case intDay\n        Case 1 strDay = &#8220;first&#8221;\n        Case 2 strDay = &#8220;second&#8221;\n        Case 3 strDay = &#8220;third&#8221;\n        Case 4 strDay = &#8220;fourth&#8221;\n        Case 5 strDay = &#8220;last&#8221;\n    End Select        \n    Wscript.Echo &#8220;Daylight Saving Time begins on the &#8221; &amp; _\n        strDay &amp; &#8221; &#8221; &amp; strDayOfWeek &amp; &#8221; in &#8221; &amp; strMonth &amp; &#8220;.&#8221;\n    intMonth =  objItem.StandardMonth\n    Select Case intMonth\n        Case 1 strMonth = &#8220;January&#8221;\n        Case 2 strMonth = &#8220;February&#8221;\n        Case 3 strMonth = &#8220;March&#8221;\n        Case 4 strMonth = &#8220;April&#8221;\n        Case 5 strMonth = &#8220;May&#8221;\n        Case 6 strMonth = &#8220;June&#8221;\n        Case 7 strMonth = &#8220;July&#8221;\n        Case 8 strMonth = &#8220;August&#8221;\n        Case 9 strMonth = &#8220;September&#8221;\n        Case 10 strMonth = &#8220;October&#8221;\n        Case 11 strMonth = &#8220;November&#8221;\n        Case 12 strMonth = &#8220;December&#8221;\n    End Select\n    intDayOfWeek = objItem.StandardDayOfWeek\n    Select Case intDayOfWeek\n        Case 0 strDayOfWeek = &#8220;Sunday&#8221;\n        Case 1 strDayOfWeek = &#8220;Monday&#8221;\n        Case 2 strDayOfWeek = &#8220;Tuesday&#8221;\n        Case 3 strDayOfWeek = &#8220;Wednesday&#8221;\n        Case 4 strDayOfWeek = &#8220;Thursday&#8221;\n        Case 5 strDayOfWeek = &#8220;Friday&#8221;\n        Case 6 strDayOfWeek = &#8220;Saturday&#8221;\n    End Select\n    intDay = objItem.StandardDay\n    Select Case intDay\n        Case 1 strDay = &#8220;first&#8221;\n        Case 2 strDay = &#8220;second&#8221;\n        Case 3 strDay = &#8220;third&#8221;\n        Case 4 strDay = &#8220;fourth&#8221;\n        Case 5 strDay = &#8220;last&#8221;\n    End Select        \n    Wscript.Echo &#8220;Standard Time begins on the &#8221; &amp; _\n        strDay &amp; &#8221; &#8221; &amp; strDayOfWeek &amp; &#8221; in &#8221; &amp; strMonth &amp; &#8220;.&#8221;\nNext\n<\/PRE>\n<P>This is all a little complicated thanks to the way this information is stored. The <B>DaylightMonth<\/B> property is an integer value representing the month (1 for January, 2 for February, etc.). The <B>DaylightDayOfWeek<\/B> property is an integer value representing the day of the week when Daylight Saving Time begins (e.g., 0 equals Sunday, 1 equals Monday, and so on.) In other words, that property tells us that Daylight Saving Time begins on a Sunday. As to <I>which<\/I> Sunday, well, that\u2019s the job of the <B>DaylightDay<\/B> property. This is yet another integer value, with 1 representing the first (in our case) Sunday of the month, 2 representing the second Sunday of the month, etc.<\/P>\n<P>Like we said, it\u2019s a bit complicated, but the end result is simple enough:<\/P><PRE class=\"codeSample\">Daylight Saving Time begins on the second Sunday in March.\n<\/PRE>\n<P>And then we repeat this process with the corresponding Standard Time properties, resulting in output similar to this:<\/P><PRE class=\"codeSample\">Standard Time begins on the first Sunday in November.\n<\/PRE>\n<P>And the great circle of life goes on.<\/P>\n<P>That should do it, AL. And you know, after giving it a little more thought, we decided to go back to the good old days of <I>Hey, Scripting Guy!<\/I>, the days when we <I>did<\/I> talk about things other than system administration scripting. Let\u2019s see, what else went on this past weekend \u2026. What\u2019s that? The Apple Cup football game between the University of Washington Huskies and the Washington State Cougars? Right \u2026 Listen, have we mentioned lately that this is a daily <I>scripting<\/I> column? The truth is, there are some things that should never be discussed in a daily scripting column.<\/P>\n<P>And there are also some things, like this year\u2019s Apple Cup, that should never be discussed at all. <\/P>\n<P>Ever.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I retrieve time zone information for a computer?&#8212; AL Hey, AL. You know, over the Thanksgiving Day weekend the Scripting Guy who writes this column took some time to think about his job, and to think of ways in which he could do that job even better. (Note to the [&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,5],"class_list":["post-63513","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-operating-system","tag-scripting-guy","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! How can I retrieve time zone information for a computer?&#8212; AL Hey, AL. You know, over the Thanksgiving Day weekend the Scripting Guy who writes this column took some time to think about his job, and to think of ways in which he could do that job even better. (Note to the [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63513","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=63513"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63513\/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=63513"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63513"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63513"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}