{"id":291,"date":"2011-06-29T00:08:23","date_gmt":"2011-06-29T00:08:23","guid":{"rendered":"http:\/\/www.sitekickr.com\/blog\/?p=291"},"modified":"2011-06-29T00:08:23","modified_gmt":"2011-06-29T00:08:23","slug":"coldfusio-dates-within-common-time-periods","status":"publish","type":"post","link":"https:\/\/www.sitekickr.com\/blog\/coldfusio-dates-within-common-time-periods\/","title":{"rendered":"ColdFusion dates within common time periods"},"content":{"rendered":"<p>I&#39;ve had to do this so many times over the years, for all sorts of different time frames, so I decided to build, document and share my algorithms. Using ColdFusion code for the examples, the following demonstrates how to obtain the start and finish dates within the time periods:<\/p>\n<ul>\n<li>Yesterday<\/li>\n<li>Today<\/li>\n<li>Last Week<\/li>\n<li>This Week<\/li>\n<li>Last Month<\/li>\n<li>This Month<\/li>\n<li>Last Quarter<\/li>\n<li>This Quarter<\/li>\n<li>Last Year<\/li>\n<li>This Year<\/li>\n<\/ul>\n<p><code>&lt;!--- YESTERDAY ---&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;Yesterday: <br \/>\n\t#DateFormat(DateAdd(&quot;d&quot;,-1,NOW()), &quot;yyyy-mm-dd&quot;)# &amp;mdash; <br \/>\n\t#DateFormat(DateAdd(&quot;d&quot;,-1,NOW()), &quot;yyyy-mm-dd&quot;)#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><\/p>\n<p>\t<code>&lt;!--- TODAY ---&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;Today: <br \/>\n\t#DateFormat(NOW(), &quot;yyyy-mm-dd&quot;)# &amp;mdash; <br \/>\n\t#DateFormat(NOW(), &quot;yyyy-mm-dd&quot;)#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><\/p>\n<p>\t<code>&lt;!--- LAST WEEK ---&gt;<br \/>\n\t&lt;cfset startDate = DateFormat(DateAdd(&quot;d&quot;,-(DayOfWeek(NOW()) + 6),NOW()), &quot;yyyy-mm-dd&quot;)&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;Last Week: <br \/>\n\t#startDate# &amp;mdash; <br \/>\n\t#DateFormat(DateAdd(&quot;d&quot;,6,startDate), &quot;yyyy-mm-dd&quot;)#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><\/p>\n<p>\t<code>&lt;!--- THIS WEEK ---&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;This Week: <br \/>\n\t#DateFormat(DateAdd(&quot;d&quot;,-(DayOfWeek(NOW())) + 1,NOW()), &quot;yyyy-mm-dd&quot;)# &amp;mdash; <br \/>\n\t#DateFormat(NOW(), &quot;yyyy-mm-dd&quot;)#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><br \/>\n\t<code><br \/>\n\t&lt;!--- LAST MONTH ---&gt;<br \/>\n\t&lt;cfset finishDate = DateFormat( DateAdd(&quot;d&quot;, -1, Year(NOW()) &amp; &quot;-&quot; &amp; Month(NOW())) &amp; &quot;-01&quot; , &quot;yyyy-mm-dd&quot;)&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;Last Month: <br \/>\n\t#Year(finishDate) &amp; &quot;-&quot; &amp; Month(finishDate) &amp; &quot;-01&quot;# &amp;mdash; <br \/>\n\t#finishDate#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><br \/>\n\t<code><br \/>\n\t&lt;!--- THIS MONTH ---&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;This Month: <br \/>\n\t#Year(NOW()) &amp; &quot;-&quot; &amp; Month(NOW()) &amp; &quot;-01&quot;# &amp;mdash; <br \/>\n\t#DateFormat(NOW(), &quot;yyyy-mm-dd&quot;)#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><br \/>\n\t<code><br \/>\n\t&lt;!--- LAST QUARTER ---&gt;<br \/>\n\t&lt;cfset lastQuarter = Ceiling(Month(Now()) \/ 3) - 1&gt;<br \/>\n\t&lt;cfif lastQuarter eq 0&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;cfset startDate = CreateDate(Year(Now()) - 1, 10, 1)&gt;<br \/>\n\t&lt;cfelse&gt;<br \/>\n\t&nbsp;&nbsp;&nbsp; &lt;cfset startDate = CreateDate(Year(Now()), (lastQuarter - 1) * 3 + 1, 1)&gt;<br \/>\n\t&lt;\/cfif&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;Last Quarter: <br \/>\n\t#DateFormat(startDate, &quot;yyyy-mm-dd&quot;)# &amp;mdash; <br \/>\n\t#DateFormat(DateAdd(&quot;d&quot;, -1, DateAdd(&quot;m&quot;, 3, startDate)), &quot;yyyy-mm-dd&quot;)#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><\/p>\n<p>\t<code>&lt;!--- THIS QUARTER ---&gt;<br \/>\n\t&lt;cfset thisQuarter = Ceiling(Month(Now()) \/ 3)&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;This Quarter: <br \/>\n\t#Year(NOW()) &amp; &quot;-&quot; &amp; ((thisQuarter - 1) * 3 + 1) &amp; &quot;-&quot; &amp; &quot;01&quot;# &amp;mdash; <br \/>\n\t#DateFormat(NOW(), &quot;yyyy-mm-dd&quot;)#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><br \/>\n\t<code><br \/>\n\t&lt;!--- LAST YEAR ---&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;Last Year: <br \/>\n\t#(Year(NOW()) - 1) &amp; &quot;-01-01&quot;# &amp;mdash; <br \/>\n\t#(Year(NOW()) - 1) &amp; &quot;-12-31&quot;#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><\/p>\n<p>\t<code>&lt;!--- THIS YEAR ---&gt;<br \/>\n\t&lt;cfoutput&gt;&lt;p&gt;This Year: <br \/>\n\t#Year(NOW()) &amp; &quot;-01-01&quot;# &amp;mdash; <br \/>\n\t#DateFormat(NOW(), &quot;yyyy-mm-dd&quot;)#&lt;\/p&gt;&lt;\/cfoutput&gt;<\/code><\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#39;ve had to do this so many times over the years, for all sorts of different time frames, so I decided to build, document and&hellip;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"amp_status":""},"categories":[15,34],"tags":[100],"_links":{"self":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/291"}],"collection":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/comments?post=291"}],"version-history":[{"count":1,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/291\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/posts\/291\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/media?parent=291"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/categories?post=291"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.sitekickr.com\/blog\/wp-json\/wp\/v2\/tags?post=291"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}