Created
November 16, 2021 22:48
-
-
Save trycf/d77435b67bab2cfd96d987b15522799b to your computer and use it in GitHub Desktop.
TryCF Gist
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<cffunction name="weekOfYear" returnType="date"> | |
<cfargument name="yearWeek" type="string"> | |
<!--- Parse out the year, the week of the year from arguments.yearWeek and default the day of week to Monday ---> | |
<cfset year = listGetAt(arguments.yearWeek, 1, "-W")> | |
<cfset woy = listGetAt(arguments.yearWeek, 2, "-W")> | |
<cfset dow = 2> | |
<!--- Calculate the number of days this year and last year for later use. ---> | |
<cfset DaysThisYear = daysInYear(CreateDate(year, 1, 1))> | |
<cfset DaysLastYear = daysInYear(CreateDate(year-1, 1, 1))> | |
<!--- Multiply week number "woy" by 7, then add the weekday number "dow" ---> | |
<cfset ordinalDate = woy*7 + dow> | |
<!--- From this sum, subtract the correction for the year: Get the weekday of 4 January and add 3 ---> | |
<cfset ordinalDate = ordinalDate - (dayOfWeek(parseDateTime("#year#-01-04", "y-M-d")) + 3)> | |
<!--- The result is the ordinal date, which can be converted into a calendar date. ---> | |
<cfif ordinalDate LT 1> | |
<!--- If the ordinal date thus obtained is zero or negative, the date belongs to the previous calendar year. ---> | |
<cfset ordinalDate = ordinalDate + daysLastYear> | |
<cfset year = year-1> | |
<cfelseif ordinalDate GT daysThisYear> | |
<!--- If it is greater than the number of days in the year, it belongs to the following year. ---> | |
<cfset ordinalDate = ordinalDate - daysThisYear> | |
<cfset year = year+1> | |
</cfif> | |
<cfreturn parseDateTime("#year#-#ordinalDate#", "y-D")> | |
</cffunction> | |
<cfoutput> | |
<!--- Testing with your example. ---> | |
2021-W46 => #weekOfYear("2021-W46")#<br><br> | |
<!--- Testing edge cases. ---> | |
2014-W1 => #weekOfYear("2014-W1")#<br> | |
2014-W52 => #weekOfYear("2014-W52")#<br> | |
2014-W53 => #weekOfYear("2014-W53")#<br><br> | |
2015-W1 => #weekOfYear("2015-W1")#<br> | |
2015-W52 => #weekOfYear("2015-W52")#<br> | |
2015-W53 => #weekOfYear("2015-W53")#<br><br> | |
2016-W1 => #weekOfYear("2016-W1")#<br> | |
2016-W52 => #weekOfYear("2016-W52")#<br> | |
2016-W53 => #weekOfYear("2016-W53")#<br><br> | |
2017-W1 => #weekOfYear("2017-W1")#<br> | |
2017-W52 => #weekOfYear("2017-W52")#<br> | |
2017-W53 => #weekOfYear("2017-W53")#<br><br> | |
2018-W1 => #weekOfYear("2018-W1")#<br> | |
2018-W52 => #weekOfYear("2018-W52")#<br> | |
2018-W53 => #weekOfYear("2018-W53")#<br><br> | |
2019-W1 => #weekOfYear("2019-W1")#<br> | |
2019-W52 => #weekOfYear("2019-W52")#<br> | |
2019-W53 => #weekOfYear("2019-W53")#<br><br> | |
2020-W1 => #weekOfYear("2020-W1")#<br> | |
2020-W52 => #weekOfYear("2020-W52")#<br> | |
2020-W53 => #weekOfYear("2020-W53")#<br><br> | |
2021-W1 => #weekOfYear("2021-W1")#<br> | |
2021-W52 => #weekOfYear("2021-W52")#<br> | |
2021-W53 => #weekOfYear("2021-W53")#<br> | |
</cfoutput> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment