string week date to coldfusion date(字符串星期日期到coldfusion日期)
问题描述
我需要像这样格式化一个字符串日期:
2021-W46
变成一些东西,我可以在冷融合中使用 parseDateTime
或 dateFormat
函数.
W46
在这种情况下是第 46 周.
我尝试的是直接将该字符串放入 parseDateTime
和 dateFormat
,但两者都给我一个错误,即我的输入不是正确的日期格式.
我该怎么做?
编辑:
我忘了说我需要那周的第一天(在我的例子中是星期一)
使用来自维基百科的算法 找到代码的工作要点..p>
<cf 函数名=weekOfYear"returnType=日期"><cfargument name=年周";类型=字符串"><!--- 从arguments.yearWeek 中解析出年份和星期,并将星期几默认为星期一---><cfset year = listGetAt(arguments.yearWeek, 1, "-W")><cfset woy = listGetAt(arguments.yearWeek, 2, "-W")><cfset dow = 2><!--- 计算今年和去年的天数,以备后用.---><cfset DaysThisYear = daysInYear(CreateDate(year, 1, 1))><cfset DaysLastYear = daysInYear(CreateDate(year-1, 1, 1))><!--- 乘以周数woy"乘以 7,然后加上工作日数字dow";---><cfset ordinalDate = woy*7 + dow><!--- 从这个总和中减去当年的修正:得到 1 月 4 日的工作日并加上 3 ---><cfset ordinalDate = ordinalDate - (dayOfWeek(parseDateTime(#year#-01-04", y-M-d")) + 3)><!--- 结果是序号日期,可以转换为日历日期.---><cfif ordinalDate LT 1><!--- 如果这样获得的序号日期为零或负数,则该日期属于上一个日历年.---><cfset ordinalDate = ordinalDate + daysLastYear><cfset year = year-1><cfelseif ordinalDate GT daysThisYear><!--- 如果大于当年的天数,则属于下一年.---><cfset ordinalDate = ordinalDate - daysThisYear><cfset 年 = 年+1></cfif><cfreturn parseDateTime("#year#-#ordinalDate#","y-D")></cffunction>
I need to format a string date like this one:
2021-W46
into something, that I can use in coldfusions parseDateTime
or dateFormat
function.
W46
in this case is week 46.
What I tried was to directly put that string into parseDateTime
and dateFormat
, but both gave me an error that my input is not a correct date format.
How can I do that?
Edit:
I forgot to mention that I need the first day of that week (in my case Monday)
Using the algorithm from Wikipedia for calculating an ordinal or month date from a week date and your input format, this function will return the Monday date from the supplied ISO week numbering format passed to the function as a string.
Calculating an ordinal or month date from a week date
Algorithm:
- Multiply the week number woy by 7.
- Then add the weekday number dow.
- From this sum subtract the correction for the year:
- Get the weekday of 4 January.
- Add 3.
- The result is the ordinal date, which can be converted into a calendar date.
- If the ordinal date thus obtained is zero or negative, the date belongs to the previous calendar year;
- if it is greater than the number of days in the year, it belongs to the following year.
A working gist of the code can be found here.
<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>
这篇关于字符串星期日期到coldfusion日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:字符串星期日期到coldfusion日期
- 失败的 Canvas 360 jquery 插件 2022-01-01
- Fetch API 如何获取响应体? 2022-01-01
- Flexslider 箭头未正确显示 2022-01-01
- CSS媒体查询(最大高度)不起作用,但为什么? 2022-01-01
- Quasar 2+Apollo:错误:找不到ID为默认的Apollo客户端。如果您在组件设置之外,请使用ProvideApolloClient() 2022-01-01
- 如何使用 JSON 格式的 jQuery AJAX 从 .cfm 页面输出查 2022-01-01
- 400或500级别的HTTP响应 2022-01-01
- Css:将嵌套元素定位在父元素边界之外一点 2022-09-07
- 使用RSelum从网站(报纸档案)中抓取多个网页 2022-09-06
- addEventListener 在 IE 11 中不起作用 2022-01-01