Ask a Jedi: What day is Wednesday?

The day after Tuesday obviously. All kidding aside, a reader pinged me today with a simple question:

How do I find the date for this week's Wednesday?
This is a bit simple with some math. Every day of the week has a corresponding number. Sunday is 1. Monday 2. Etc. If Wednesday is 4, then we know how many days we are away from Wednesday. So today (the day I'm writing this blog entry) is Tuesday. The day of the week is 3. The difference is 1. So if I wanted to create a date object for Wednesday, I would need to add one to today. If today were Thursday, I'd need to subtract one.

So lets abstract that a bit. We want to end up with a number that we can add to today to get to Wednesday. If we take the current day of the week (3), subtract Wednesday (4), we end up with -1. If we multiply this value by -1, we get 1. Which is valid.

Now assume Thursday. Thursday is day 5. 5-4 is 1. 1 times -1 is -1. That also gives us a valid modifier to get to Wednesday.

So the logic is: (DOW-4) * -1. We can wrap this up nicely like so:

<cfset thisWednesday = dateAdd("d", (dayOfWeek(now()) - 4) * -1, now())>

For another version of this, consider PrevOccOfDow and NextOccOfDow from CFLib. These do not find the current week's Wednesday unless it just so happens Wednesday would be immediately following or before the date in question, but these alternate versions may be exactly what the reader had in mind.

Archived Comments

Comment 1 by Matthew posted on 6/5/2007 at 6:57 PM

Thanks Ray!

Comment 2 by Rick Osborne posted on 6/5/2007 at 8:02 PM

I would have said <cfset thisWednesday=dateAdd("d",4-dayOfWeek(now()), now())>, but each to their own. ;-)

Getting the *next* Wednesday isn't too much trickier:

<cfset nextWednesday = dateAdd("d",(11-dayOfWeek(now())) MOD 7, now())>

Comment 3 by Ben Nadel posted on 6/5/2007 at 9:15 PM

You know I love my date math:

<!--- 1: sunday, 7: staurday. --->
<cfset intDay = 4 />

#DateFormat(
(Fix( Now() ) - DayOfWeek( Now() )) + intDay
)#

It's all numbers to me :)

Comment 4 by Dale Fraser posted on 6/6/2007 at 9:40 AM

Ray,

Your code doesn't work.

<!--- Sunday = 1, Satuarday = 7 --->
<cfset today = createDate(2007, 6, 9) />
<cfset friday = dateAdd("d", (dayOfWeek(today) - 6) * -1, today) />
<cfoutput>#friday#</cfoutput>

Outputs

{ts '2007-06-08 00:00:00'}

So it gave me the previous Friday

Comment 5 by Dale Fraser posted on 6/6/2007 at 9:43 AM

Rick,

Yours has the same problem

<!--- Sunday = 1, Satuarday = 7 --->
<cfset today = createDate(2007, 6, 9) />
<cfset friday = dateAdd("d", 6-dayOfWeek(now()), now()) />
<cfoutput>#friday#</cfoutput>

Comment 6 by Dale Fraser posted on 6/6/2007 at 10:45 AM

You probably have no idea what i'm on about, now I realise the problem lies in that my week isn't Sunday - Satuarday (not sure who's is). The one I wanted was Satuarday - Friday.

<!--- Sunday = 1, Satuarday = 7 --->
<cfset today = createDate(2007, 6, 16) />
<cfset dow = dayOfWeek(today) />
<cfif dow eq 7>
<cfset friday = dateAdd("d", 6, today) />
<cfelse>
<cfset friday = dateAdd("d", (dow - 6) * -1, today) />
</cfif>
<cfoutput>#friday#</cfoutput>

Comment 7 by Raymond Camden posted on 6/6/2007 at 3:28 PM

Dale - no. The user didn't want the next X. He wanted this weeks X. See how in the blog entry, at the end, I talk about another function to get prev/last day of week dates?

Comment 8 by Dale Fraser posted on 6/6/2007 at 4:15 PM

Ray,

I wanted the same thing, but my week is different.

I think dayOfWeek should have a paramater to specify what the first day of the week is.

Comment 9 by Raymond Camden posted on 6/6/2007 at 5:02 PM

Dale - check CFLib. There is an ISODOW there I believe.

Comment 10 by Ben Nadel posted on 6/6/2007 at 6:42 PM

@Dale,

I wrote something like that a little while ago in answer to someone's question. It takes an optional argument for which day is the first of the week:

http://www.bennadel.com/ind...

Comment 11 by Norm posted on 6/28/2007 at 1:01 AM

Thanks for taking the time to create this blog. It really helped me figure out how to get my scheduling program working properly.

Code On!

Comment 12 by mac32 posted on 3/23/2009 at 9:02 AM

how can I use this to find out what the second Wed of each month is. I have a project where the client wants a calculation done only on the 2nd wed of each month.

Comment 13 by Raymond Camden posted on 3/24/2009 at 5:14 AM