假設有個初始日期
<cfset yourdate = "2011-01-18">
找上週的第一天 (週日)
<CFSET preWeekFirstday = #dateadd("d",-(DayOfWeek(yourdate)+6),yourDate)#>
找上週的最後一天(週六)
<CFSET preWeekEndday = #dateadd("d",-DayOfWeek(yourdate),yourDate)#>
找當週的第一天 (週日)
<CFSET weekFirstday = #dateadd("d",-(DayOfWeek(yourdate)-1),yourDate)#>
找當週的最後一天(週六)
<CFSET weekEndday = #dateadd("d",7-DayOfWeek(yourdate),yourDate)#>
找下週的第一天 (週日)
<CFSET nextWeekFirstday = #dateadd("d",7-(DayOfWeek(yourdate)-1),yourDate)#>
找下週的最後一天(週六)
<CFSET nextWeekEndday = #dateadd("d",14-DayOfWeek(yourdate),yourDate)#>
找某週次的第一天 (週日)
<CFSET weekNum = 5> <!--- 假設要找第五週的資料 --->
<CFSET yearFday = year(yourdate)&"-1-1"> <!--- 該年第一天 --->
<CFSET xFirstday = #dateadd("d",fix((1-DayOfWeek(yearFday))+(weekNum-1)*7),yearFday)#>
找某週次的最後一天(週六)
<CFSET xEndday = #dateadd("d",fix((7-DayOfWeek(yearFday))+(weekNum-1)*7),yearFday)#>
完整程式碼參考:
<!--- 設定初始日期 --->
<cfset yourDate = "2011-01-18">
<!--- 找上週的第一天(週日) --->
<CFSET preWeekFirstday = #dateadd("d",-(DayOfWeek(yourdate)+6),yourDate)#>
<!--- 找上週的最後一天(週六) --->
<CFSET preWeekEndday = #dateadd("d",-DayOfWeek(yourdate),yourDate)#>
<!--- 找當週的第一天(週日) --->
<CFSET weekFirstday = #dateadd("d",-(DayOfWeek(yourdate)-1),yourDate)#>
<!--- 找當週的最後一天(週六) --->
<CFSET weekEndday = #dateadd("d",7-DayOfWeek(yourdate),yourDate)#>
<!--- 找下週的第一天(週日) --->
<CFSET nextWeekFirstday = #dateadd("d",7-(DayOfWeek(yourdate)-1),yourDate)#>
<!--- 找下週的最後一天(週六) --->
<CFSET nextWeekEndday = #dateadd("d",14-DayOfWeek(yourdate),yourDate)#>
<!--- 找該年某週次的第一天(週日) --->
<CFSET weekNum = 5> <!--- 假設要找第五週的資料 --->
<CFSET yearFday = year(yourdate)&"-1-1"> <!--- 該年第一天 --->
<CFSET xFirstday = #dateadd("d",fix((1-DayOfWeek(yearFday))+(weekNum-1)*7),yearFday)#>
<!--- 找該年某週次的最後一天(週六) --->
<CFSET xEndday = #dateadd("d",fix((7-DayOfWeek(yearFday))+(weekNum-1)*7),yearFday)#>
<!--- 找當週週次 --->
<CFSET yourWeek = week(yourdate)>
<!--- 結果輸出 --->
初 始 日 期:#DateFormat(yourdate, "YYYY/MM/DD")# <hr>
上週的第一天 (週日):#DateFormat(preWeekFirstday, "YYYY/MM/DD")# <Br>
上週的最後一天(週六):#DateFormat(preWeekEndday, "YYYY/MM/DD")# <Br><Br>
當週的第一天 (週日):#DateFormat(weekFirstday, "YYYY/MM/DD")# <Br>
當週的最後一天(週六):#DateFormat(weekEndday, "YYYY/MM/DD")# <Br><Br>
下週的第一天 (週日):#DateFormat(nextWeekFirstday, "YYYY/MM/DD")# <Br>
下週的最後一天(週六):#DateFormat(nextWeekEndday, "YYYY/MM/DD")# <Br><Br>
第#weekNum#週的第一天 (週日):#DateFormat(xFirstday, "YYYY/MM/DD")# <Br>
第#weekNum#週的最後一天(週六):#DateFormat(xEndday, "YYYY/MM/DD")# <Br><Br>
當 週 週 次:#yourWeek# <Br>
輸出結果: