字符串星期日期到coldfusion日期

string week date to coldfusion date(字符串星期日期到coldfusion日期)

本文介绍了字符串星期日期到coldfusion日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要像这样格式化一个字符串日期:

2021-W46

变成一些东西,我可以在冷融合中使用 parseDateTimedateFormat 函数.

W46 在这种情况下是第 46 周.

我尝试的是直接将该字符串放入 parseDateTimedateFormat,但两者都给我一个错误,即我的输入不是正确的日期格式.

我该怎么做?

编辑:

我忘了说我需要那周的第一天(在我的例子中是星期一)

解决方案

使用来自维基百科的算法 找到代码的工作要点..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:

  1. Multiply the week number woy by 7.
  2. Then add the weekday number dow.
  3. From this sum subtract the correction for the year:
    • Get the weekday of 4 January.
    • Add 3.
  4. 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日期