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日期
基础教程推荐
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 直接将值设置为滑块 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01