'coldfusion get the date for next Monday
I have a requirement to work out the date for the next monday.
What ever given day the user is browsing on my app, I need to know the next Monday or specified day which is provided as a string.
For example.
Todays date is 11/07/2014 or for you americans 07/11/2014 and I have an variable of Monday to work with.
So I need to work out how to work this out by providing a function as a string.
I have seen alot of functions for calculating the date based on other dates, but nothing that i can pass in an argument of the day that I require.
Any ideas greatly appreciated
Solution 1:[1]
Based on Leigh's answer in the link How to find the most recent past Monday?
<cfif dayOfWeek(currentDate) gt 1>
<cfset NextMonday = dateAdd("d", 9 - dayOfWeek(currentDate), currentDate)>
<cfelse>
<cfset NextMonday = dateAdd("d", 1, currentDate)>
</cfif>
This is based on the assumption that if currentDate is Monday, it will get the Next Monday.
Solution 2:[2]
<cfset anydate = "01/25/2022">
<cfset nextDate = dateAdd("d",dayOfWeek(anydate)-dayOfWeek(now())+7, dateformat(now(),"mm/dd/yyyy"))>
<cfdump var="#nextDate#">
It should equal '02/01/2022'.
This will predict the next date of your provided date's (anydate), day of the week. This will ignore the current week.
This will calculate the next date even though it's within the current week.
<cfif dayOfWeek(anydate) GT dayOfWeek(now())>
<cfset nextDate = dateAdd("d",dayOfWeek(anydate)-dayOfWeek(now()), dateformat(now(),"mm/dd/yyyy"))>
<cfelse>
<cfset nextDate = dateAdd("d",dayOfWeek(anydate)-dayOfWeek(now())+7, dateformat(now(),"mm/dd/yyyy"))>
</cfif>
*American's date format. Your format might be something different if on the other side of the pond.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Community |
| Solution 2 |
