Ask Ben: Shifting ColdFusion's DayOfWeek() Functionality

Posted May 8, 2007 at 5:48 PM by Ben Nadel

Tags: ColdFusion, Ask Ben

Macbuoy, in one of his comments, asked about shifting the day of the week functionality that ColdFusion provides. By default, ColdFusion assigns Sunday as day One and Saturday as day Seven. To shift this value, such as having Monday be day One and having Sunday be day Seven, requires wrapping ColdFusion's DayOfWeek() method in a custom, user defined function:

  • <cffunction
  • name="MyDayOfWeek"
  • access="public"
  • returntype="numeric"
  • output="false"
  • hint="Returns our proxy day of week value.">
  •  
  • <!--- Define arguments. --->
  • <cfargument
  • name="Date"
  • type="date"
  • required="true"
  • hint="The date that we are using to get the day of week."
  • />
  •  
  • <cfargument
  • name="FirstDayOfWeek"
  • type="numeric"
  • required="false"
  • default="2"
  • hint="This is the day (1 = Sunday) that we are treating as the first day of the week."
  • />
  •  
  •  
  • <!---
  • Translate the actual day of the week to the pseudo
  • day of the week using the passed in FirstDayOfWeek.
  • --->
  • <cfreturn
  • (
  • (
  • (
  • DayOfWeek( ARGUMENTS.Date ) +
  • (7 - ARGUMENTS.FirstDayOfWeek)
  • ) MOD 7
  • ) +
  • 1
  • ) />
  • </cffunction>

This ColdFusion user defined function takes an optional argument, FirstDayOfWeek, which will define which day is the first day of the week. It defaults to 2 which defines Monday as day One, but this can be any value between 1 and 7 (I am not doing any error checking on that).

To test, let's run over two weeks worth of dates to see how this ColdFusion user defined function translates the original DayOfWeek() to our business-related day of week:

  • <!---
  • Loop over every day in these two weeks to
  • demonstrate translated days of the week.
  • --->
  • <cfloop
  • index="dtDay"
  • from="05/06/2007"
  • to="05/19/2007"
  • step="1">
  •  
  • #DateFormat( dtDay )# -
  • #DayOfWeek( dtDay )#
  •  
  • -to-
  •  
  • <!--- Use translated DayOfWeek(). --->
  • #MyDayOfWeek( dtDay )#
  •  
  • <br />
  •  
  • </cfloop>

This gives us the following output:

06-May-07 - 1 -to- 7
07-May-07 - 2 -to- 1
08-May-07 - 3 -to- 2
09-May-07 - 4 -to- 3
10-May-07 - 5 -to- 4
11-May-07 - 6 -to- 5
12-May-07 - 7 -to- 6
13-May-07 - 1 -to- 7
14-May-07 - 2 -to- 1
15-May-07 - 3 -to- 2
16-May-07 - 4 -to- 3
17-May-07 - 5 -to- 4
18-May-07 - 6 -to- 5
19-May-07 - 7 -to- 6

As you can see, we are successfully translating Monday (day 2) as the first day of the week.




Reader Comments

May 9, 2007 at 4:20 AM // reply »
18 Comments

good work, this should really have been an optional parameter to the function in the first place. CF's default to Sunday for the first day of the week isn't logical, especially for us in the UK. It's also against ISO 8601.
http://en.wikipedia.org/wiki/ISO_8601


May 9, 2007 at 7:54 AM // reply »
11,314 Comments

How is it in the UK? Is Monday the first calendar day over there?


May 9, 2007 at 10:27 AM // reply »
18 Comments

yes indeed.


May 9, 2007 at 10:56 AM // reply »
32 Comments

I'm not sure if I'm doing something wrong, but when I apply this to my version of your most recent Month calendar, I'm getting dates misaligned with day of week. I'm going to start from scratch with your example and add the new wrapper and see how it turns out.

Will report.


May 9, 2007 at 10:58 AM // reply »
11,314 Comments

Interesting. I did not try applying it to the calendar. I will give it a go at lunch, see what I can come up with.


May 9, 2007 at 11:05 AM // reply »
32 Comments

yeah. When I apply this to this:

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

calendar grid alignment is off. my first guess is that I'm applying the wrapper too often. . .I need to understand the MyDayOfWeek math better to do this right.


May 9, 2007 at 1:47 PM // reply »
11,314 Comments

@Macbuoy,

Check this out:

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

You're probably making a small mistake somewhere. This took practically no changes. I just replaced the DayOfWeek() calls and reordered the calendar header.


Aug 14, 2009 at 2:11 PM // reply »
1 Comments

If you take a look at the calendar I am interested in the members of the club to be able to submit a post and it appears on the day instead of a click into. I am not sure if that is what yours does because the demo will not allow add/submit?


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
Jun 19, 2013 at 2:01 PM
Experimenting With The Amazon Simple Storage Service (S3) API Using ColdFusion
I have coincidentally been beating my head against the S3 API for the last week or so. One big "gotcha" I had to work around was file names and paths containing spaces. Remember to URL Enco ... read »
Jun 19, 2013 at 1:27 PM
Using Slice(), Substring(), And Substr() In Javascript
very good article. By the way IE supports negative values in substr or slice in verson 10. ... read »
Jun 19, 2013 at 11:33 AM
Filter vs. ngHide With ngRepeat In AngularJS
In your assessment, is it correct to say that given a list of say 500 items its more performant to use the `ngHide` method over the `filter` method? ... read »
Jun 19, 2013 at 10:18 AM
ColdFusion Path Usage And Manipulation Overview
Anyone happen to know if the file created by getTempFile will be automatically removed at any point? Nothing mentioned in the docs, and restarting CF doesn't remove them, so it seems it needs manu ... read »
Jun 19, 2013 at 9:41 AM
Working With Inherited Collections In AngularJS
I actually just ran into this same situation with a demo I was putting together. Your implementation of multi-lvl $scope's > Mine :) ... read »
Jun 19, 2013 at 8:17 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
@Prateek, to match a word or text you should use .toContain('word') that's a jasmine reference. website is : http://pivotal.github.io/jasmine/ ... read »
Jun 19, 2013 at 8:10 AM
My Experience With AngularJS - The Super-heroic JavaScript MVW Framework
Hi Guys, Actually i am doing e2e test of angular js of my project but i am not getting one thing that is how to press enter key through the test when my form is filled as i am not using a button but ... read »
Jun 18, 2013 at 9:20 PM
Mapping AngularJS Routes Onto URL Parameters And Client-Side Events
I couldn't find examples of passing multiple arguments using the when() routing statement so figured out through trial and error that you can pass multiple arguments using the following format: .whe ... read »
InVision App - Prototyping Made Beautiful With Prototyping Tools