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
Thanks Ray!
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())>
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 :)
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
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>
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>
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?
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.
Dale - check CFLib. There is an ISODOW there I believe.
@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...
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!
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.
Here ya go:
http://www.cflib.org/udf/Ge...