{"id":63563,"date":"2007-11-17T00:43:00","date_gmt":"2007-11-17T00:43:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/17\/hey-scripting-guy-how-can-i-set-the-date-and-time-on-a-computer\/"},"modified":"2007-11-17T00:43:00","modified_gmt":"2007-11-17T00:43:00","slug":"hey-scripting-guy-how-can-i-set-the-date-and-time-on-a-computer","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-set-the-date-and-time-on-a-computer\/","title":{"rendered":"Hey, Scripting Guy! How Can I Set the Date and Time on 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 set the date and time on a computer?<BR><BR>&#8212; KM<\/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, KM. Well, it\u2019s true: today is the last day of <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov07\/technet\/scriptcenter\/topics\/teched07\/default.mspx\"><B>TechEd IT Forum<\/B><\/A>. That also means that, for the Scripting Guys, the party is over: tomorrow morning they hop on a plane and head back for home. And, needless to say, back to the real world: jobs, car payments, mortgages, families, the whole nine yards.<\/P>\n<P>Gee, we can hardly wait.<\/P>\n<P>Anyway, we decide to celebrate our last day in Barcelona the only way the Scripting Guys know how to celebrate: by writing a script that can set the date and time on a computer. Stand back, everyone; it\u2019s time for the Scripting Guys to party:<\/P><PRE class=\"codeSample\">dtmNewDateTime = &#8220;20071115132000.000000-480&#8221;<\/p>\n<p>strComputer = &#8220;.&#8221;<\/p>\n<p>Set objWMIService = GetObject(&#8220;winmgmts:{(Systemtime)}\\\\&#8221; &amp; strComputer &amp; &#8220;\\root\\cimv2&#8221;)<\/p>\n<p>Set colOSes = objWMIService.ExecQuery(&#8220;Select * From Win32_OperatingSystem&#8221;)<\/p>\n<p>For Each objOS In colOSes\n    objOS.SetDateTime dtmNewDateTime\nNext\n<\/PRE>\n<P>A word or two of caution before we explain how this script works. This script will, indeed, change the time on the computer, and that could be a problem if the computer is a member of an Active Directory domain. Active Directory relies on computer times being synchronized; if a computer\u2019s time strays too far from the domain controller time you won\u2019t be able to log on to the network. (However, you\u2019ll still be able to log on to the computer itself.) Likewise, Active Directory does its best to keep computer times in synch; even if you <I>can<\/I> log on to the domain there\u2019s a good chance that, sooner or later, Active Directory will update the time for you. That might not be a problem for you, and it won\u2019t be a problem at all if your computer isn\u2019t part of an Active Directory domain. Just something you should keep in mind.<\/P>\n<TABLE id=\"EKD\" 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>. That said, changing computer times like this is often used for testing purposes, even within an Active directory domain.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So how <I>does<\/I> the script work? Well, step 1 is to assign a new date and time to a variable named dtmNewDateTime; that\u2019s what we do in the very first line of code:<\/P><PRE class=\"codeSample\">dtmNewDateTime = &#8220;20071115130000.000000-480&#8221;\n<\/PRE>\n<P>And you know what? You\u2019re right: that <I>doesn\u2019t<\/I> look very much like a date-time value, does it? That\u2019s because WMI uses the so-called Universal Time Coordinate (UTC) format. As strange as a UTC value might look, it really <I>is<\/I> a date-time value:<\/P>\n<TABLE border=\"0\" cellSpacing=\"0\" cellPadding=\"0\">\n<TBODY>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The first four characters (<B>2007<\/B>) represent the year.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The next two characters (<B>11<\/B>) represent the month.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The next two characters (<B>15<\/B>) represent the day.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The next two characters (<B>13<\/B>) represent the hour in 24-hour format. For you Americans out there, \u201c13 o\u2019clock\u201d is the same as 1:00 PM.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The next two characters (<B>00<\/B>) represent the seconds.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The <B>.000000<\/B> represents the milliseconds.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P>The <B>-480<\/B> represents the \u201coffset\u201d from Greenwich Mean Time. -480 means that the time zone this computer lives in is 8 hours (480 minutes) earlier than Greenwich Mean Time. In other words, if it\u2019s 12:00 noon in Redmond, WA (where this computer lives) then it must be 8:00 PM (20:00) in Greenwich, England.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<TABLE id=\"EOF\" 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>. How the heck are you supposed to know your offset from Greenwich Mean Time? Take a peek at the <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/guide\/sas_wmi_onfu.mspx\"><B>Microsoft Windows 2000 Scripting Guide<\/B><\/A> for the answer.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As you can probably see, we created our own UTC value for this script; we did that so that this script would work on versions of Windows prior to Windows XP. If you\u2019re running Windows XP or a later version of Windows you can use the <B>WbemScripting.SWbemDateTime<\/B> object to create a UTC value for you:<\/P><PRE class=\"codeSample\">Set objSWbemDateTime = CreateObject(&#8220;WbemScripting.SWbemDateTime&#8221;)<\/p>\n<p>objSWbemDateTime.SetVarDate #11\/15\/2007 1:20 PM#, True\ndtmDateTime = objSWbemDateTime.Value<\/p>\n<p>Wscript.Echo dtmDateTime\n<\/PRE>\n<P>For more information, see the <I>Hey, Scripting Guy!<\/I> column <A href=\"http:\/\/www.microsoft.com\/technet\/technetmag\/issues\/2006\/07\/ScriptingGuy\/default.aspx\"><B>I<\/B><B>t\u2019s About Time (Oh, and About Dates, Too)<\/B><\/A> in <I>TechNet Magazine<\/I>.<\/P>\n<P>Getting a proper UTC value is actually the difficult part of this script. Once we have this value we then connect to the WMI service on the local computer. (Although we could also run this script against a remote machine; just assign the name of that remote computer to the variable strComputer.) After that we use this line of code to retrieve a collection of all the operating systems currently in use on the computer:<\/P><PRE class=\"codeSample\">Set colOSes = objWMIService.ExecQuery(&#8220;Select * From Win32_OperatingSystem&#8221;)\n<\/PRE>\n<TABLE id=\"EXG\" 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>. Why did we say \u201ccurrently in use\u201d rather than installed? That\u2019s easy: that\u2019s how the <B>Win32_OperatingSystem<\/B> class works. This class only returns information about the operating system currently in use; it does <I>not<\/I> return information about any other operating systems that might be installed on a multi-boot system.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>From there we set up a For Each loop to through each item in the collection (although there will never be more than one item in that collection). Inside that loop all we have to do is call the <B>SetDateTime<\/B> method, passing SetDateTime the UTC value we created:<\/P><PRE class=\"codeSample\">objOS.SetDateTime dtmNewDateTime\n<\/PRE>\n<P>Just like that, the time will get changed on the computer. And everyone will live happily ever after.<\/P>\n<P>We hope that answers your question, KM. As for the Scripting Guys, we have one final day of booth duty, and then one final chance to eat churros with a cup of melted chocolate and whipped cream. One final chance to \u2013 sigh. If you happen to be at IT Forum today, swing by <B>booth 22<\/B> and say hi to the Scripting Guys. They could use a little cheering up.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I set the date and time on a computer?&#8212; KM Hey, KM. Well, it\u2019s true: today is the last day of TechEd IT Forum. That also means that, for the Scripting Guys, the party is over: tomorrow morning they hop on a plane and head back for home. And, needless [&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-63563","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 set the date and time on a computer?&#8212; KM Hey, KM. Well, it\u2019s true: today is the last day of TechEd IT Forum. That also means that, for the Scripting Guys, the party is over: tomorrow morning they hop on a plane and head back for home. And, needless [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63563","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=63563"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63563\/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=63563"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63563"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63563"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}