Getting Week, Month, And Year Dates Based On A Given Date In ColdFusion

Posted May 7, 2007 at 9:30 AM by Ben Nadel

Tags: ColdFusion

When working with any sort of ColdFusion event or calendar system, you will need to get dates based on other dates. In my experience, given a date, it is often necessary to get the start and end dates of week containing the given date; the start and end dates of the month containing the given date; the start and end dates of the calendar (display) month containing the given date; and, the start and end dates of the year containing the given date. The purpose of this post is just to demonstrate how to get those date values.

For all of these, assume we built the "Today" date using:

  • <!---
  • Get the current date. We are going to Fix()
  • ColdFusion's Now() date/time stamp to remove the
  • time, leaving us with just the date value.
  • --->
  • <cfset dtToday = Fix( Now() ) />

Getting the start and end date of the week:

  • <!--- Get the week start date. --->
  • <cfset dtWeekStart = (dtToday - DayOfWeek( dtToday ) + 1) />
  •  
  • <!--- Get the week end date. --->
  • <cfset dtWeekEnd = (dtToday + (7 - DayOfWeek( dtToday ))) />
  •  
  •  
  • <!--- Output the dates: --->
  • Today: #DateFormat( dtToday )#<br />
  • Week Start: #DateFormat( dtWeekStart )#<br />
  • Week End: #DateFormat( dtWeekEnd )#<br />

...gives us:

Today: 07-May-07
Week Start: 06-May-07
Week End: 12-May-07

Getting the start and end date of the month:

  • <!--- Get the month start date. --->
  • <cfset dtMonthStart = (dtToday - Day( dtToday ) + 1) />
  •  
  • <!--- Get the month end date. --->
  • <cfset dtMonthEnd = (
  • dtToday + (DaysInMonth( dtToday ) - Day( dtToday ))
  • ) />
  •  
  •  
  • <!--- Output the dates: --->
  • Today: #DateFormat( dtToday )#<br />
  • Month Start: #DateFormat( dtMonthStart )#<br />
  • Month End: #DateFormat( dtMonthEnd )#<br />

...gives us:

Today: 07-May-07
Month Start: 01-May-07
Month End: 31-May-07

Getting the start and end date of the year:

  • <!---
  • Get the year start date. These dates never change with
  • exception of the year. The first date of the year will
  • always be January 1st.
  • --->
  • <cfset dtYearStart = CreateDate(
  • Year( dtToday ),
  • 1,
  • 1
  • ) />
  •  
  •  
  • <!---
  • Get the year end date. These dates never change with
  • exception of the year. The last date of the year will
  • always be December 31st.
  • --->
  • <cfset dtYearEnd = CreateDate(
  • Year( dtToday ),
  • 12,
  • 31
  • ) />
  •  
  •  
  • <!--- Output the dates: --->
  • Today: #DateFormat( dtToday )#<br />
  • Year Start: #DateFormat( dtYearStart )#<br />
  • Year End: #DateFormat( dtYearEnd )#<br />

...gives us:

Today: 07-May-07
Year Start: 01-Jan-07
Year End: 31-Dec-07

Getting the start and end date of the displayed calendar month (may or may not spill over into other numeric months):

  • <!---
  • The calendar display dates are ever slightly more
  • complicated. It helps to work off of the previously
  • calculated month start/end dates to make them easier
  • to work with.
  • --->
  • <cfset dtCalendarMonthStart = (
  • dtMonthStart - DayOfWeek( dtMonthStart ) + 1
  • ) />
  •  
  • <!--- Get the end date. --->
  • <cfset dtCalendarMonthEnd = (
  • dtMonthEnd + (7 - DayOfWeek( dtMonthEnd ))
  • ) />
  •  
  •  
  • <!--- Output the dates: --->
  • Today: #DateFormat( dtToday )#<br />
  • Cal. Month Start: #DateFormat( dtCalendarMonthStart )#<br />
  • Cal. Month End: #DateFormat( dtCalendarMonthEnd )#<br />

...give us:

Today: 07-May-07
Cal. Month Start: 29-Apr-07
Cal. Month End: 02-Jun-07

Notice that in all of the outputs, we are using DateFormat(). The reason for this is that all of the date math that we are doing results in numeric dates rather than string-formatted dates. This is all good and fine if you are looping and querying using CFQueryParam, but if you try to use IsDate() on the resultant dates, this will fail; you much use IsNumericDate() with numeric dates (note: string-formatted dates will also work with IsNumericDate()).




Reader Comments

May 7, 2007 at 10:05 AM // reply »
79 Comments

Last time I had to do this (which was quite some time ago), I don't remember it being this simple. Now, I'm not quite sure of calculating the start and end date of the year (does it change from 1/1/year(now()) and 12/31/year(now())?) but calculating the beginning of the week given a date I recall was a PITA back then.

You should wrap at least that into a function on cflib, if one doesn't already exist.


May 7, 2007 at 5:34 PM // reply »
11,238 Comments

@Sam,

Let me see what I can do... I know that biggest leap for me came when I found out that I could mess with dates with out using DateAdd() and DateDiff() methods... the code just became so much cleaner (but perhaps slightly less intuitive, especially if you are not used to doing date-math).


May 8, 2007 at 9:08 AM // reply »
79 Comments

Well, I never liked using the date functions - they are just unintuitive in my opinion. I think getting around it like you have done improves the readability and understanding of what the code is doing (and I never thought about trying it out myself).


May 8, 2007 at 2:58 PM // reply »
32 Comments

I'm using parts of this solution along with an entry on general calendaring to produce a calendar with Monday as the week start and Saturday and Sunday combined in a column. I've got the layout right, but:

What if I wanted to start the week on Monday (2) and end on Sunday (1). I'm thinking that I need to, somehow, override the DayOfWeek to that Monday is 1 and Sunday is 7. I think I'm onto something here and think it could be quite simple, but I just can't seem to get down to it.

I'm also guessing that there might be--should be--a CF setting, hopefully local to a template, that will allow this reassignment/shifting of 1 to Monday.


May 8, 2007 at 5:52 PM // reply »
11,238 Comments

@Macbuoy,

Check this out (I think this might help you out):

http://www.bennadel.com/index.cfm?dax=blog:691.view


Aug 21, 2009 at 8:41 AM // reply »
1 Comments

i know its been a while to the post but i was trying to loop through last three week including current week..somehow the loop gives wrong set of dates

basically trying to get
8/16 to 8/22 8/9 to 8/15 8/2 to 8/8 7/26 to 8/1

any help will be appreciated...thanks


Sep 6, 2009 at 1:37 PM // reply »
11,238 Comments

@Mili,

I use the CFLoop tag to loop over dates quite effectively.

http://www.bennadel.com/blog/136-Using-CFLoop-To-Loop-Over-ColdFusion-Dates.htm


Post A Comment

Comment Etiquette: Please do not post spam. Please keep the comments on-topic. Please do not post unrelated questions or large chunks of code. And, above all, please be nice to each other - we're trying to have a good conversation here.

Please review the following issues:

Author Name:


Author Email:

Author Website:

Comment:

Supported HTML tags for formatting: <strong>bold</strong>   <em>italic</em>   <code>code</code>







  • Help Wanted - Find Your Next ColdFusion Job
Ben Nadel's Company - Epicenter Consulting Recent Blog Comments
May 19, 2013 at 2:31 PM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
It's funny really just how well that image describes the way I would imagine most people that go with angular for some project is. I have had a similar roller-coaster ride with it as well, but not qu ... read »
May 17, 2013 at 7:42 PM
HashKeyCopier - An AngularJS Utility Class For Merging Cached And Live Data
Ben - thanks so much for posting these Angular articles and findings, they've been a huge help towards learning one of the more 'complex' JavaScript frameworks out there (IMO). I have been using Angu ... read »
May 16, 2013 at 5:01 PM
UPDATE: Parsing CSV Data Files In ColdFusion With csvToArray()
Your code was the closest thing I've found to obtaining some direction for converting ISO fields to values that CF can translate properly. Thank you for posting! ... read »
May 15, 2013 at 10:37 PM
Very Simple Pusher And ColdFusion Powered Chat
hi id making plz easy ... read »
May 15, 2013 at 6:07 PM
Making SOAP Web Service Requests With ColdFusion And CFHTTP
Ben, you once again saved my bacon at work. Thank you, thank you, thank you! ... read »
May 15, 2013 at 4:15 PM
What If All User Interface (UI) Data Came In Reports?
@Josh, Thanks! @Ben, I definitely recommend the David West book "Object Thinking" I've been quoting from. It goes deeply into the philosophy and history of OO programming. His breadth ... read »
May 15, 2013 at 11:36 AM
Ask Ben: Print Part Of A Web Page With jQuery
I found this helpfull when you need to keep (refresh) the original parent page after closing the iframe child print dialog (Hoping you're not using a form at this time so it won't submit again): On ... read »
May 14, 2013 at 7:13 PM
What If All User Interface (UI) Data Came In Reports?
@Jonah, If there's any books you'd recommend on the subject of domain modelling, I'd love to hear it. I just downloaded the free PDF of "Domain Driven Design Quickly". Figured I'd give it ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools