{"id":56213,"date":"2008-02-13T22:49:00","date_gmt":"2008-02-13T22:49:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2008\/02\/13\/hey-scripting-guy-how-can-i-convert-pacific-standard-time-to-my-local-time\/"},"modified":"2008-02-13T22:49:00","modified_gmt":"2008-02-13T22:49:00","slug":"hey-scripting-guy-how-can-i-convert-pacific-standard-time-to-my-local-time","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-convert-pacific-standard-time-to-my-local-time\/","title":{"rendered":"Hey, Scripting Guy! How Can I Convert Pacific Standard Time to My Local Time?"},"content":{"rendered":"<p><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Question\" height=\"34\" alt=\"Hey, Scripting Guy! Question\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/q-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/> <\/p>\n<p>Hey, Scripting Guy! I really enjoyed the 2007 Winter Scripting Games, but one thing I noticed is that you listed the deadlines for all the events using Redmond time. I understand that you can\u2019t list deadlines using the local time for every time zone in the world; however, maybe you could show us how to write a script that could convert Redmond time to our local time. Any chance of you doing that for the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/default.mspx\"><b>2008 Winter Scripting Games<\/b><\/a>?<\/p>\n<p>&#8212; OD<\/p>\n<p><img decoding=\"async\" height=\"5\" alt=\"Spacer\" src=\"https:\/\/devblogs.microsoft.com\/scripting\/wp-content\/uploads\/sites\/29\/2019\/05\/spacer.gif\" width=\"5\" border=\"0\" \/><img decoding=\"async\" class=\"nearGraphic\" title=\"Hey, Scripting Guy! Answer\" height=\"34\" alt=\"Hey, Scripting Guy! Answer\" src=\"https:\/\/devblogs.microsoft.com\/wp-content\/uploads\/sites\/29\/2019\/02\/a-for-powertip.jpg\" width=\"34\" align=\"left\" border=\"0\" \/><a href=\"http:\/\/go.microsoft.com\/fwlink\/?linkid=68779&amp;clcid=0x409\"><img decoding=\"async\" class=\"farGraphic\" title=\"Script Center\" height=\"288\" alt=\"Script Center\" src=\"http:\/\/img.microsoft.com\/library\/media\/1033\/technet\/images\/scriptcenter\/ad.jpg\" width=\"120\" align=\"right\" border=\"0\" \/><\/a> <\/p>\n<p>Hey, OD. Considering the fact that it\u2019s been just about a year since you sent this email you might have thought, \u201cI bet the Scripting Guys forgot all about the time zone issue with the Scripting Games.\u201d Well, rest assured, OD, that the Scripting Guys rarely, if ever, forget anything. That doesn\u2019t mean that we\u2019re actually going to follow up on something; heavens no. But we won\u2019t forget about it.<\/p>\n<p>And even if we did, the Scripting Editor would no doubt swoop down on her broomstick and offer a \u2026 gentle \u2026 reminder.<\/p>\n<p>She\u2019s very good at that.<\/p>\n<p>So no, we didn\u2019t forget about this question; instead, we\u2019ve been sitting on it until the time was ripe for answering it. And now that the Scripting Games are just two days away (the \u201copening ceremonies\u201d are scheduled for Friday, February 15<sup>th<\/sup>), well, the time is about as ripe as it\u2019s ever going to be.<\/p>\n<p>So <i>can<\/i> we show you a script that canconvert Redmond time to your local time? You know what? Not only <i>can<\/i> we show you a script like that, we <i>will<\/i> show you a script like that. In fact, here it is right now:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"Set objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")Set colTime = objWMIService.ExecQuery(\"Select * From Win32_TimeZone\")For Each objTime in colTime    intBias = objTime.BiasNextdtmRedmondTime = #2\/19\/2008 3:00 PM#dtmGreenwichTime = DateAdd(\"n\", 480, dtmRedmondTime)dtmLocalDeadline = DateAdd(\"n\",intBias, dtmGreenwichTime)Wscript.Echo dtmLocalDeadline<\/pre>\n<p>You know, you\u2019re right: this would have been a good event in the Scripting Games, wouldn\u2019t it? That\u2019s because it isn\u2019t particularly hard, but it <i>is<\/i> a little bit tricky. And that\u2019s typically the type of event we like to include in the Scripting Games.<\/p>\n<p>Oh, well. Maybe next year.<\/p>\n<p>For now, though, let\u2019s see if we can explain how this baby works. As you can see, we kick things off by connecting to the WMI service on the local computer. Could we run this script against a <i>remote<\/i> computer? Sure, although if that remote computer is in the same time zone as your local machine there isn\u2019t much point in running the script against a remote computer. But if you <i>do<\/i> want to run the script against a remote machine (perhaps to help a long-distance friend convert Redmond time to <i>their<\/i> local time) simply assign the name of the remote computer to the variable strComputer, like so:<\/p>\n<pre class=\"codeSample\">strComputer = \"atl-ws-001\"<\/pre>\n<p>After connecting to the WMI service we then use the following line of code to retrieve a collection of all the time zones that a computer belongs to:<\/p>\n<pre class=\"codeSample\">Set colTime = objWMIService.ExecQuery(\"Select * From Win32_TimeZone\")<\/pre>\n<p>Note that, in the universe we live in, a computer can belong to only a single time zone; thus this query is going to return only a single item. Could there be a parallel universe in which the accepted laws of physics don\u2019t apply and computers <i>could<\/i> belong to more than one time zone at the same time? Hey, don\u2019t ask us; we\u2019re just Scripting Guys.<\/p>\n<p>Incidentally, the fact that <b>Win32_TimeZone<\/b> returns only a single item makes this class a \u201csingleton\u201d class. And yes, that would have been a good question to pose during the Scripting Games, too. Dang; if we\u2019re not careful, today\u2019s column is going to be way better and way more challenging than the actual Scripting Games!<\/p>\n<p>Which would mark the first time this column was better than <i>anything<\/i>.<\/p>\n<p>After we issue our query we next set up a For Each loop to loop through each item in the collection. And yes, we know: because Win32_TimeZone is a singleton class it has only one item in it. Nevertheless, a collection is a collection, and we still have to use a For Each loop to walk through it, regardless of the number of items in that collection.<\/p>\n<table class=\"dataTable\" id=\"EYE\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Or at least that\u2019s the way it <i>used<\/i> to be, in this universe. However, in Windows Vista it\u2019s possible to directly access an item in a collection without using a For Each loop. For more information, see our Script Center article <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/topics\/vista\/indexer.mspx\"><b>WMI Indexer<\/b><\/a>.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>Inside this For Each loop we do just one thing; we grab the value of the <b>Bias<\/b> property and store it in a variable named intBias:<\/p>\n<pre class=\"codeSample\">intBias = objTime.Bias<\/pre>\n<p>What\u2019s the Bias property? Well, the Bias property tells us the difference (in minutes) between local time and Universal Time Coordinate time. That\u2019s the official SDK explanation; in more practical terms, the Bias property tells us the difference (in minutes) between local time and Greenwich Mean Time. For example, when it\u2019s 10:00 AM in London (home of the <a href=\"http:\/\/en.wikipedia.org\/wiki\/Prime_Meridian\" target=\"_blank\"><b>Prime Meridian<\/b><\/a>) it\u2019s 2:00 AM in Redmond. Redmond time is 8 hours earlier than Greenwich Mean Time, meaning the Pacific Standard Time time zone has a Bias of -480 minutes.<\/p>\n<p>But why do we even need to know the Bias for the local time zone? We\u2019ll explain that in just a minute.<\/p>\n<p>Before we get to that, however, we need to create a date-time value representing the date and time in Redmond. For example, if the deadline for a particular event (say, Event 1 in the <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/sdchallenge.mspx\"><b>Sudden Death Challenge<\/b><\/a>) is February 19, 2008 at 3:00 PM we can create a date-time value for that deadline using this line of code:<\/p>\n<pre class=\"codeSample\">dtmRedmondTime = #2\/19\/2008 3:00 PM#<\/pre>\n<table class=\"dataTable\" id=\"EEG\" cellSpacing=\"0\" cellPadding=\"0\">\n<thead><\/thead>\n<tbody>\n<tr class=\"record\" vAlign=\"top\">\n<td class=\"\">\n<p class=\"lastInCell\"><b>Note<\/b>. Why the pound signs (#) surrounding our date-time value? That helps ensure that VBScript treats the value as a date and not as a string.<\/p>\n<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<div class=\"dataTableBottomMargin\"><\/div>\n<p>That brings us to this line of code:<\/p>\n<pre class=\"codeSample\">dtmGreenwichTime = DateAdd(\"n\", 480, dtmRedmondTime)<\/pre>\n<p>What are we doing here? Well, here we\u2019re using the <b>DateAdd<\/b> function to add 480 minutes to the date-time value we just created. (When working with the DateAdd function, the <b>\u201cn\u201d<\/b> parameter represents minutes. Don\u2019t use <b>\u201cm\u201d<\/b> to add minutes; the \u201cm\u201d parameter would end up adding 480 <i>months<\/i>, not 480 minutes.) <i>Why<\/i> are we adding 480 minutes to our Redmond date-time value? Well, as we noted earlier, Redmond time is 8 hours earlier than Greenwich Mean Time. To convert Redmond time to Greenwich Mean Time we need to add 8 hours (480 minutes).<\/p>\n<p>But why do we even <i>want<\/i> to convert Redmond time to Greenwich Mean Time? Well, directly converting a time from one time zone to another can be a little tricky; that\u2019s due, in part, to the fact that some time zones have a negative bias (like -480) and others have a positive value (like 120). Rather than mess around with all this we decided to perform this operation using two steps: we convert Redmond time to its equivalent time in Greenwich Mean Time; then convert the Greenwich time to our local time. Granted, it\u2019s an extra step, but it\u2019s also a little easier to code. And much easier for people to visualize.<\/p>\n<p>As you might expect, then, our next step is to convert the Greenwich time to the local time. That\u2019s what this line of code does:<\/p>\n<pre class=\"codeSample\">dtmLocalDeadline = DateAdd(\"n\",intBias, dtmGreenwichTime)<\/pre>\n<p>This time we\u2019re adding (or subtracting, if we have a negative bias) minutes to Greenwich time. How many minutes are we adding? To tell you the truth, we don\u2019t know; we\u2019re relying on the variable intBias to take care of that for us.<\/p>\n<p>See how this all works? We start out with the following Redmond time value: <\/p>\n<pre class=\"codeSample\">2\/19\/2008 3:00 PM<\/pre>\n<p>When we convert this value to Greenwich Mean Time we get <i>this<\/i> value:<\/p>\n<pre class=\"codeSample\">2\/19\/2008 11:00 PM<\/pre>\n<p>We then use our local time zone bias to convert Greenwich time to local time. For example, suppose we live in Athens, Greece. Athens is two hours ahead of Greenwich Mean Time; that means it has a bias of 120 minutes. If we add 120 minutes to our Greenwich time we get this:<\/p>\n<pre class=\"codeSample\">2\/20\/2008 1:00 AM<\/pre>\n<p>In other words, when it\u2019s 3:00 PM Monday in Redmond it\u2019s 1:00 AM Tuesday in Athens. That\u2019s how you can convert one of our Pacific Standard Time deadlines to a local time deadline. And you can do this regardless of your location.<\/p>\n<p>At least in <i>this<\/i> universe.<\/p>\n<p>By the way, if you ever need to know the current time in Redmond you can use this script:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"Set objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")Set colTime = objWMIService.ExecQuery(\"Select * From Win32_TimeZone\")For Each objTime in colTime    intBias = objTime.Bias    intBias = intBias * -1NextdtmLocalTime = NowdtmGreenwichTime = DateAdd(\"n\", intBias, dtmLocalTime)dtmRedmondTime = DateAdd(\"n\", -480, dtmGreenwichTime)Wscript.Echo dtmRedmondTime<\/pre>\n<p>What, exactly, does this script do? Well, it first uses the <b>Now<\/b> function to grab the current date and time; it then employs this line of code, and time zone bias, to convert the current date and time to Greenwich Mean Time:<\/p>\n<pre class=\"codeSample\">dtmGreenwichTime = DateAdd(\"n\", intBias, dtmLocalTime)<\/pre>\n<p>Note that, in our For Each loop, we multiplied intBias by minus 1. Why? Because we\u2019re no longer going from Greenwich time to local time like we did in our first script; instead, we\u2019re going from local time to Greenwich time. Because we\u2019ve reversed direction we need to reverse the sign, turning a bias like 120 minutes to -120 minutes.<\/p>\n<p>Finally, we add -480 minutes to Greenwich Mean Time to determine the time in Redmond:<\/p>\n<pre class=\"codeSample\">dtmRedmondTime = DateAdd(\"n\",-480, dtmGreenwichTime)<\/pre>\n<p>If it\u2019s 7:43 AM on February 20<sup>th<\/sup> in Athens we should get back the following value for Redmond time:<\/p>\n<pre class=\"codeSample\">2\/19\/2008 9:43 PM<\/pre>\n<p>Pretty cool, huh?<\/p>\n<p>By the way, if that all seems a little too complicated, well, there <i>is<\/i> an alternative: you can simply take a look at <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/timezones.mspx\"><b>this page<\/b><\/a>, where we\u2019ve taken the deadline times (8:00 AM for the main Scripting Games events; 3:00 PM for the Sudden Death Challenge events) and converted them to local times for various cities and time zones throughout the world.<\/p>\n<p>And yes, it would have been <i>way<\/i> easier to simply point people to this Web page rather than write a couple scripts (and an entire column). But you know what they say: when all you have is a hammer everything looks like a nail. And when you\u2019re a Scripting Guy \u2013 well, everything looks like a nail to us, too. But since we don\u2019t have a hammer, we don\u2019t have much choice except to write scripts instead.<\/p>\n<p>Speaking of scripts, are you literally counting the minutes until the Scripting Games begin? Well, relax, and stop counting. Assuming that the Games officially open at 8:00 AM PST on February 15, 2008, you can use the following script to tell you how many minutes are left until the Games begin:<\/p>\n<pre class=\"codeSample\">strComputer = \".\"Set objWMIService = GetObject(\"winmgmts:\\\\\" &amp; strComputer &amp; \"\\root\\cimv2\")Set colTime = objWMIService.ExecQuery(\"Select * From Win32_TimeZone\")For Each objTime in colTime    intBias = objTime.Bias    intBias = intBias * -1NextdtmLocalTime = NowdtmGreenwichTime = DateAdd(\"n\", intBias, dtmLocalTime)dtmRedmondTime = DateAdd(\"n\", -480, dtmGreenwichTime)dtmOpeningDay = #2\/15\/2008 8:00 AM#Wscript.Echo DateDiff(\"n\", dtmRedmondTime, dtmOpeningDay)<\/pre>\n<p>We\u2019ll see everyone on Friday, February 15<sup>th<\/sup>. What time on Friday? Well, <i>that<\/i> you should now be able to figure out for yourself.<\/p>\n<p>Or you can just look at <a href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/funzone\/games\/games08\/timezones.mspx\"><b>this Web page<\/b><\/a>. Good luck in the Games, everyone!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! I really enjoyed the 2007 Winter Scripting Games, but one thing I noticed is that you listed the deadlines for all the events using Redmond time. I understand that you can\u2019t list deadlines using the local time for every time zone in the world; however, maybe you could show us how to [&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,3,4,5],"class_list":["post-56213","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-scripting","tag-dates-and-times","tag-scripting-guy","tag-scripting-techniques","tag-vbscript"],"acf":[],"blog_post_summary":"<p>Hey, Scripting Guy! I really enjoyed the 2007 Winter Scripting Games, but one thing I noticed is that you listed the deadlines for all the events using Redmond time. I understand that you can\u2019t list deadlines using the local time for every time zone in the world; however, maybe you could show us how to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56213","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=56213"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/56213\/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=56213"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=56213"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=56213"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}