Skip to main content
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: Gary Boyle
Ben Nadel at Scotch On The Rocks (SOTR) 2011 (Edinburgh) with: Gary Boyle

Ask Ben: Shifting ColdFusion's DayOfWeek() Functionality

By
Published in , Comments (10)

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.

Want to use code from this post? Check out the license.

Reader Comments

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.

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?

3 Comments

It's interesting, but i am facing a problem that it does not work for 'FirstDayOfWeek' function. I pass 4 to function but it does not work plz help me

1 Comments

I'am not sure what happens here?
When I output the #MyDayOfWeek# variable in a page, it display as follows:
cfApplication2ecfm296765156$funcMYDAYOFWEEK@863bfb

I have included this cffunction code in my application.cfm file.

I have a support page where, on a week to week basis the correct support person will be displayed with the correct support tel numbers and such.
So on even weeks Joe is on standby and the odd weeks Jerry.
I am using this piece of code to check to see if the week is even:
<cfset checkweekeven = #Week(Now())# mod 2 is 0>
Then in an "if statement" I use checkweekeven to display the correct support person.
The problem here is that on sunday the person for the next week is allready displayed. But we want that to happen on monday in stead.

Anyone have any idea how I can fix this with this piece of code?

Best Regards Richy

I believe in love. I believe in compassion. I believe in human rights. I believe that we can afford to give more of these gifts to the world around us because it costs us nothing to be decent and kind and understanding. And, I want you to know that when you land on this site, you are accepted for who you are, no matter how you identify, what truths you live, or whatever kind of goofy shit makes you feel alive! Rock on with your bad self!
Ben Nadel