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:
Launch code in new window » Download code as text file »
Getting the start and end date of the week:
Launch code in new window » Download code as text file »
...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:
Launch code in new window » Download code as text file »
...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:
Launch code in new window » Download code as text file »
...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):
Launch code in new window » Download code as text file »
...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()).
Download Code Snippet ZIP File
Comments (5) | Post Comment | Ask Ben | Permalink | Other Searches | Print Page
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.
Posted by Sam on May 7, 2007 at 10:05 AM
@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).
Posted by Ben Nadel on May 7, 2007 at 5:34 PM
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).
Posted by Sam on May 8, 2007 at 9:08 AM
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.
Posted by macbuoy on May 8, 2007 at 2:58 PM
@Macbuoy,
Check this out (I think this might help you out):
http://www.bennadel.com/index.cfm?dax=blog:691.view
Posted by Ben Nadel on May 8, 2007 at 5:52 PM