如何获取前一周的星期一日期和前一周的星期五日期?

How can I get the Monday's date of the previous week and Friday's date of the previous week?

我用的是ColdFusion 2016,需要什么代码才能得到上一周的前周一和前一周的前周五?

下面的代码获取前一个星期一,但我想要周的前一个星期一。周五也一样。

<cfset currentDate = Now() /> 
<cfset mostRecentMonday = dayOfWeek(currentDate) gt 1 ? dateAdd("d", 2- 
 dayOfWeek(currentDate), currentDate) : dateAdd("d", -6, currentDate) />

您只需要简化您的解决方案。

<cfset currentDate = Now() /> 
<cfset obj = {}>
<cfset todaydayOfWeek = dayOfWeek(currentDate)>
<cfset mondayOffset = (2-todaydayOfWeek)-7>
<cfset fridayOffset = (6-todaydayOfWeek)-7>
<cfset obj.mostRecentMonday = dateAdd("d", mondayOffset, currentDate)>
<cfset obj.mostRecentFriday = dateAdd("d", fridayOffset, currentDate)>
<cfdump var="#obj#">

DEMO

你可以得到上周五,

<cfset today=dayofweek(now())>
<span>Last Friday is:</span>
<cfif today eq 7>
    <span>#dateformat(dateAdd("d",-1,now()))#</span>
<cfelse>
    <span>#dateformat(dateAdd ("d",-(1+today),now()))#</span>
</cfif>

上周一:

<cfif today eq 7>
   <span>#dateformat(dateAdd("d",-5,now()))#</span>
<cfelse>
   <span>#dateformat(dateAdd ("d",-(5+today),now()))#</span><br>
</cfif>

@user1669296,确保你的最后一个星期一也 return 错误 value.For 例如。今天(2018 年 9 月 6 日)是星期一,如果您 运行 该代码将 return 今天日期而不是上星期一。所以也请指正。