{"id":68193,"date":"2006-01-12T12:59:00","date_gmt":"2006-01-12T12:59:00","guid":{"rendered":"https:\/\/blogs.technet.microsoft.com\/heyscriptingguy\/2006\/01\/12\/how-can-i-determine-which-day-of-the-year-a-date-falls-on\/"},"modified":"2006-01-12T12:59:00","modified_gmt":"2006-01-12T12:59:00","slug":"how-can-i-determine-which-day-of-the-year-a-date-falls-on","status":"publish","type":"post","link":"https:\/\/devblogs.microsoft.com\/scripting\/how-can-i-determine-which-day-of-the-year-a-date-falls-on\/","title":{"rendered":"How Can I Determine Which Day of the Year a Date Falls On?"},"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 determine which day of the year a date falls on? For example, March 2, 2006 is the 61<SUP>st<\/SUP> day of the year.<BR><BR>&#8212; TW<\/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, TW. You know, we almost feel guilty about answering this question. Why? Well, after all, we can answer it by writing just one line of code:<\/P><PRE class=\"codeSample\">Wscript.Echo DatePart(&#8220;y&#8221;, &#8220;3\/2\/2006&#8221;)\n<\/PRE>\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>. So if it\u2019s that easy why do we <I>almost<\/I> feel guilty about answering the question; why don\u2019t we actually <I>feel<\/I> guilty? Well, to be honest, we believe the Scripting Guys deserve a break once in awhile. After all, look at a typical Scripting Guys day: we might have to write a Scripting Week song or put together a crossword puzzle; for that matter, we might be forced to play around with Microsoft Agent or Windows Media Player. This is obviously arduous and back-breaking work, and yet we do it day in and day out. (Well, except for the extended vacation time we all just finished taking.) You see what we have to go through on a daily basis; surely you can cut us some slack every now and then?<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>As far as determining the day of the year for a particular date, well, needless to say that doesn\u2019t require much effort: we simply call the <B>DatePart<\/B> function and pass it two parameters:<\/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><B>\u201cy\u201d<\/B>, which tells DatePart that we want to know the day of the year.<\/P><\/TD><\/TR>\n<TR>\n<TD class=\"listBullet\" vAlign=\"top\">\u2022<\/TD>\n<TD class=\"listItem\">\n<P><B>\u201c3\/2\/2006\u201d<\/B>, which tells DatePart which date we need information for.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<P>That\u2019s all we have to do. In turn, DatePart calculates the day of the year and the script echoes the value &#8211; 61 &#8211; back to us.<\/P>\n<P>What\u2019s cool about DatePart is that it can retrieve other date-related information besides day of the year. For example, want to know the <I>week<\/I> of the year that 3\/2\/2006 falls in? Here you go:<\/P><PRE class=\"codeSample\">Wscript.Echo DatePart(&#8220;ww&#8221;, &#8220;3\/2\/2006&#8221;)\n<\/PRE>\n<P>In this case DatePart returns a 9, meaning that 3\/2\/2006 falls in the ninth week of 2006. How about the quarter:<\/P><PRE class=\"codeSample\">Wscript.Echo DatePart(&#8220;q&#8221;, &#8220;3\/2\/2006&#8221;)\n<\/PRE>\n<P>Did someone ask what day of the week 3\/2\/2006 falls on? Here\u2019s a script that can help you with that:<\/P><PRE class=\"codeSample\">intDay = DatePart(&#8220;w&#8221;, &#8220;3\/2\/2006&#8221;)<\/p>\n<p>Select Case intDay\n    Case 1\n        Wscript.Echo &#8220;Sunday&#8221;\n    Case 2\n        Wscript.Echo &#8220;Monday&#8221;\n    Case 3\n        Wscript.Echo &#8220;Tuesday&#8221;\n    Case 4\n        Wscript.Echo &#8220;Wednesday&#8221;\n    Case 5\n        Wscript.Echo &#8220;Thursday&#8221;\n    Case 6\n        Wscript.Echo &#8220;Friday&#8221;\n    Case 7\n        Wscript.Echo &#8220;Saturday&#8221;\nEnd Select\n<\/PRE>\n<TABLE id=\"EIE\" 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>. If you can\u2019t stand the suspense any longer, 3\/2\/2006 falls on a Thursday.<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>Here\u2019s a complete list of all the different date parts that DatePart can return:<\/P>\n<TABLE id=\"EUE\" class=\"dataTable\" cellSpacing=\"0\" cellPadding=\"0\">\n<THEAD><\/THEAD>\n<TBODY>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\"><B>Setting<\/B><\/P><\/TD>\n<TD>\n<P class=\"lastInCell\"><B>Description<\/B><\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">yyyy<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Year<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">q<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Quarter<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">m<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Month<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">y<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Day of the year<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">d<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Day<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">w<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Day of the week<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">ww<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Week of the year<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">h<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Hour<\/P><\/TD><\/TR>\n<TR class=\"evenRecord\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">n<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Minute<\/P><\/TD><\/TR>\n<TR class=\"record\" vAlign=\"top\">\n<TD>\n<P class=\"lastInCell\">s<\/P><\/TD>\n<TD>\n<P class=\"lastInCell\">Second<\/P><\/TD><\/TR><\/TBODY><\/TABLE>\n<DIV class=\"dataTableBottomMargin\"><\/DIV>\n<P>And now, if you\u2019ll excuse us, we have another grueling day to try and get through. Should we have filet mignon <I>again<\/I> at lunch, or should we try something new today? It\u2019s a tough job, but someone has to do it.<\/P>\n<TABLE id=\"E2G\" 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>. Please don\u2019t point out to our managers that no one <I>has<\/I> to do this. Thanks!<\/P><\/TD><\/TR><\/TBODY><\/TABLE><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Hey, Scripting Guy! How can I determine which day of the year a date falls on? For example, March 2, 2006 is the 61st day of the year.&#8212; TW Hey, TW. You know, we almost feel guilty about answering this question. Why? Well, after all, we can answer it by writing just one line of [&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-68193","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 determine which day of the year a date falls on? For example, March 2, 2006 is the 61st day of the year.&#8212; TW Hey, TW. You know, we almost feel guilty about answering this question. Why? Well, after all, we can answer it by writing just one line of [&hellip;]<\/p>\n","_links":{"self":[{"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68193","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=68193"}],"version-history":[{"count":0,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/posts\/68193\/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=68193"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/categories?post=68193"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/devblogs.microsoft.com\/scripting\/wp-json\/wp\/v2\/tags?post=68193"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}