{"id":69283,"date":"2005-07-29T11:37:00","date_gmt":"2005-07-29T11:37:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2005\/07\/29\/how-can-i-tell-if-a-date-falls-within-a-specified-time-period\/"},"modified":"2005-07-29T11:37:00","modified_gmt":"2005-07-29T11:37:00","slug":"how-can-i-tell-if-a-date-falls-within-a-specified-time-period","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-tell-if-a-date-falls-within-a-specified-time-period\/","title":{"rendered":"How Can I Tell if a Date Falls within a Specified Time Period?"},"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! How can I tell if a date falls within a specified time period?<BR><BR>&#8212; JW<\/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, JW. By amazing coincidence on our way to work this morning we heard two different commercials, from two different companies, touting \u201cChristmas in July.\u201d That prompted us to answer your question as soon as we got to the office. After all, if these companies had a copy of the script you\u2019re looking for, then maybe they\u2019d know that Christmas <I>doesn\u2019t<\/I> come in July.<\/P>\n<TABLE id=\"E3C\" 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>. Of course, we could be wrong about that, so maybe you should send all the Scripting Guys a present anyway, just to be on the safe side.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>So how do we know that Christmas doesn\u2019t come in July? We simply ran the following script:<\/P><PRE class=\"codeSample\">dtmStartDate = #7\/1\/2005#\ndtmEndDate = #7\/31\/2005#\ndtmTargetDate = #12\/25\/2005#<\/p>\n<p>If dtmTargetDate &gt;= dtmStartDate AND dtmTargetDate &lt;= dtmEndDate Then\n    Wscript.Echo &#8220;The target date is within the specified range.&#8221;\nElse\n    Wscript.Echo &#8220;The target date is not within the specified range.&#8221;\nEnd If\n<\/PRE>\n<P>As you can see, we begin by assigning values to three variables. The variables dtmStartDate and dtmEndDate represent our date range; in this case, July 1, 2005 through July 31, 2005. The variable dtmTargetDate is the date in question: we want to know if this day (Christmas Day) comes in July.<\/P>\n<P>To do that, we need to know if the target date is greater than or equal to the start date <I>and<\/I> if the target date is less than or equal to the end date. Here\u2019s the line of code that checks for that:<\/P><PRE class=\"codeSample\">If dtmTargetDate &gt;= dtmStartDate AND dtmTargetDate &lt;= dtmEndDate Then\n<\/PRE>\n<P>In order to be true a date must meet both conditions. December 25, 2005 is greater than or equal to the start date of July 1, 2005, so our target date meets condition 1. However, December 25, 2005 is <I>not<\/I> less than or equal to July 31, 2005. The target date fails condition 2, which means that the statement is not true. (Remember, <I>both<\/I> conditions must be true.) As a result, we know that Christmas <I>doesn\u2019t<\/I> come in July, and we echo a message to that effect. Had both conditions been true, we would have echoed the jolly holiday message, \u201cThe target date is within the specified range.\u201d<\/P>\n<P>Hope that helps, JW. Merry Christmas to all and to all a good night.<\/P><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I tell if a date falls within a specified time period?&#8212; JW Hey, JW. By amazing coincidence on our way to work this morning we heard two different commercials, from two different companies, touting \u201cChristmas in July.\u201d That prompted us to answer your question as soon as we got 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-69283","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! How can I tell if a date falls within a specified time period?&#8212; JW Hey, JW. By amazing coincidence on our way to work this morning we heard two different commercials, from two different companies, touting \u201cChristmas in July.\u201d That prompted us to answer your question as soon as we got to [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69283","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=69283"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/69283\/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=69283"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=69283"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=69283"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}