{"id":63533,"date":"2007-11-22T00:48:00","date_gmt":"2007-11-22T00:48:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2007\/11\/22\/hey-scripting-guy-how-can-i-tell-if-a-time-occurred-in-the-morning-or-the-evening\/"},"modified":"2007-11-22T00:48:00","modified_gmt":"2007-11-22T00:48:00","slug":"hey-scripting-guy-how-can-i-tell-if-a-time-occurred-in-the-morning-or-the-evening","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/hey-scripting-guy-how-can-i-tell-if-a-time-occurred-in-the-morning-or-the-evening\/","title":{"rendered":"Hey, Scripting Guy! How Can I Tell If a Time Occurred in the Morning or the Evening?"},"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! Given a time of day, is there an easy way to tell whether that\u2019s a morning time or an evening time?<BR><BR>&#8212; HG<\/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, HG. Can you feel the excitement in the year, that sense of anticipation, the crackling energy and the flow of adrenalin? That can only mean one thing: the annual <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/nov06\/hey1122.mspx\"><B>Turducken Bowl<\/B><\/A> is upon us.<\/P>\n<P>On the off-chance that some of you have never heard of the annual Turducken Bowl, a few years ago the Scripting Guy who writes this column, the Scripting Son, and a pair of Scripting Nephews played a very spirited (and bruising) game of touch football on Thanksgiving Day. What started out as a way to pass the time until we could (finally!) sit down to eat has now become a holiday tradition: four downs to go the length of whatever field we\u2019re playing on; 7 points per touchdown; first team to score 100 points wins. As is the case with most family activities, there is considerable controversy over which team holds the upper hand. As far as the Scripting Guy who writes this column is concerned, his team trails 4 wins to 3.<\/P>\n<P>So how did last year\u2019s game go? Well, remember, the important thing isn\u2019t whether you win or lose; the important thing is just the sheer joy of taking part. Not that the Scripting Guy who writes this column actually experienced <I>any<\/I> kind of joy last year, let alone sheer joy. How did last year\u2019s game go? Let\u2019s put it this way: on the opening play of the game, the Scripting Guy who writes this column went out for a pass. His Scripting Nephew teammate, playing quarterback, flung the ball downfield, only to have the wind grab hold of the football and carry it over the fence and into the neighbor\u2019s backyard.<\/P>\n<P>Believe it or not, things actually got <I>worse<\/I> after that.<\/P>\n<P>Unfortunately, things don\u2019t promise to go much better this year. Due to some weird bureaucratic procedures that the Scripting Guy who writes this column doesn\u2019t even <I>pretend<\/I> to understand, it was decided that this year the team rosters would be expanded by one player each. The Scripting Son and the Scripting Nephew (the nephew who <I>didn\u2019t<\/I> throw a football over the fence), who won last year\u2019s game in a rout, get to add one of the Scripting Brothers, a Scripting Brother who\u2019s actually pretty good at football. In return, the Scripting Guy who writes this column and the Script Nephew (the one who <I>did<\/I> throw a football over the fence) get to add \u2026 one of the Scripting Significant Others. Is that a fair deal? Let\u2019s put it this way: that\u2019s about as fair as \u2013<\/P>\n<P>You know what? Maybe we shouldn\u2019t put it that way after all. Instead, maybe we should just keep quiet and not get ourselves into any further trouble.<\/P>\n<P>Besides, we can always hope that the Significant Other breaks her leg before the game starts.<\/P>\n<P>Just kidding, dear. No one wants you to break your leg.<\/P>\n<P>A twisted or sprained ankle would work just fine.<\/P>\n<P>Anyway, the Scripting Guy who writes this column is busy trying to come up with a game plan that will help his team pull off the upset of the century. Has he come up with anything yet? No, not yet, or at least not anything that doesn\u2019t involve kidnapping all the players on the other team. And even <I>then<\/I> he\u2019s not convinced that his team could win.<\/P>\n<P>One of the strategies the Scripting Guy who writes this column briefly considered was to write a script that could tell whether a specified time occurred in the evening or the afternoon. He discarded that strategy after he had trouble figuring out how that would help him win a football game. On the bright side, however, (and trust us, some of us are desperately searching for a bright side to all this) that strategy <I>did<\/I> help him to answer today\u2019s question:<\/P><PRE class=\"codeSample\">dtmTime = #11\/21\/2007 1:57 PM#<\/p>\n<p>intHour = Hour(dtmTime)<\/p>\n<p>If intHour &gt; 11 Then\n    Wscript.Echo &#8220;This is an afternoon\/evening time.&#8221;\nElse\n    Wscript.Echo &#8220;This is a morning time.&#8221;\nEnd If\n<\/PRE>\n<P>You\u2019re right: this <I>is<\/I> a very simple little script, isn\u2019t it? (And it\u2019s merely a coincidence that the Scripting Guy who writes this column picked a question that could be answered with a simple little script on the one day that he gets to leave work as soon as the column is done.) The script starts out by assigning a date-time value to a variable named dtmTime; in case, you\u2019re wondering, the pound signs (#) that surround the value tell VBScript that this is a date-time value and not some other type of value:<\/P><PRE class=\"codeSample\">dtmTime = #11\/21\/2007 1:57 PM#\n<\/PRE>\n<P>So how do we know whether this time occurred in the morning (i.e., before 12:00 noon) or in the afternoon\/evening (i.e., <I>after<\/I> 12:00 noon)? Well, <I>we<\/I> can do that easily enough; after all, we can see the <I>PM<\/I> designator stuck on the end. But how is the <I>script<\/I> supposed to know if this is a morning or evening time?<\/P>\n<P>Here\u2019s how:<\/P><PRE class=\"codeSample\">intHour = Hour(dtmTime)\n<\/PRE>\n<P>All we\u2019re doing here is using the <B>Hour<\/B> function to grab just the hour portion of our date-time value. The Hour function returns an integer value between 0 and 23, representing the hour in 24-hour time; in our sample script, that means we\u2019ll get back a 13 (because, in the 24-hour format, 1:57 PM is equivalent to 13:57). All we have to do then is determine whether the hour value we got back is greater than 11:<\/P><PRE class=\"codeSample\">If intHour &gt; 11 Then\n<\/PRE>\n<P>If this is true (that is, if the hour is between 12 and 23, inclusive) then we echo back the fact that we have an afternoon\/evening time. If this is false (that is, if the hour is between 0 and 11, inclusive) then we echo back the fact that this is a morning time. That\u2019s all we have to do.<\/P>\n<P>If only figuring out a way to win a football game was that easy, huh?<\/P>\n<P>The nice thing about using this approach is that it will work regardless of the time format we\u2019re using. For example, suppose we assigned a date-time value to the variable dtmTime using <I>this<\/I> line of code:<\/P><PRE class=\"codeSample\">dtmTime = FormatDateTime(#11\/21\/2007 13:57#, vbShortTime)\n<\/PRE>\n<P>Thanks to the <B>FormatDateTime<\/B> function, in this case dtmTime will be equal to 13:57; there won\u2019t <I>be<\/I> a PM stuck at the end of the value. But that\u2019s OK, but our script doesn\u2019t rely on the AM\/PM designator. Instead, it just grabs the hour (13) and goes from there.<\/P>\n<P>Speaking of going somewhere, it\u2019s time to hit the road and get ready for another Turducken Bowl. Like we said, this year\u2019s game should be especially, uh, \u2026 interesting \u2026. <\/P>\n<P>Oh, well. Maybe the Scripting Guy who writes this column will get lucky and break <I>his<\/I> leg before the game starts. But that\u2019s probably too much to hope for. See you all on Monday.<\/P>\n<TABLE id=\"EZF\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P><B>Note<\/B>. In case you\u2019re wondering, the Turducken Bowl is named after the turducken, a legendary Thanksgiving dish made by stuffing a chicken inside a duck, stuffing the duck inside a turkey, then somehow cooking and eating the thing. Actually, we just <I>assumed<\/I> that this was a legendary dish until last year, when we got <A href=\"http:\/\/www.microsoft.com\/technet\/scriptcenter\/resources\/qanda\/dec06\/hey1201.mspx\"><B>email from a reader<\/B><\/A> who\u2019s actually eaten turducken. To the Scripting Guy who writes this column, having eaten a plate of turducken is on par with having eaten a plate of Bigfoot or Loch Ness Monster. <\/P>\n<P>By the way, if you <I>have<\/I> eaten a plate of Bigfoot or Loch Ness Monster, drop us a line; we\u2019d love to hear about <I>that<\/I><I><\/I>experience.<\/P>\n<P>And for those of you outside the US, those of you still puzzling over the notion of stuffing a chicken inside a duck inside a turkey, the answer to your question is this: yes, only in America.<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! Given a time of day, is there an easy way to tell whether that\u2019s a morning time or an evening time?&#8212; HG Hey, HG. Can you feel the excitement in the year, that sense of anticipation, the crackling energy and the flow of adrenalin? That can only mean one thing: the annual [&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-63533","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! Given a time of day, is there an easy way to tell whether that\u2019s a morning time or an evening time?&#8212; HG Hey, HG. Can you feel the excitement in the year, that sense of anticipation, the crackling energy and the flow of adrenalin? That can only mean one thing: the annual [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63533","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=63533"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/63533\/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=63533"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=63533"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=63533"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}