我很尴尬地承认,在涉及日期和日期逻辑ColdFusion时,我并不是最伟大的。
<!---checks frequency for form schedule and sets datepart. RecordType_Frequency is a column in database daily, weekly, monthly etc.--->
<CFSWITCH expression="#RecordType_Frequency#">
<CFCASE value="Daily">
<CFSET datepart = "d">
</CFCASE>
<CFCASE value="Weekly">
<CFSET datepart = "ww">
</CFCASE>
<CFCASE value="Monthly">
<CFSET datepart = "m">
</CFCASE>
<CFCASE value="Quarterly">
<CFSET datepart = "q">
</CFCASE>
<CFCASE value="Yearly">
<CFSET datepart = "yyyy">
</CFCASE>
</CFSWITCH>
<!---setting dates based on database values for when the form should schedule--->
<!---enddate Uses the RecordType_Frequency_StartDate column from the database which is a date in the past. Coefficient is a stored db value for the frequency 1,2 etc. for could scheduled every 1 year, 2 year --->
<cfset enddate = datediff(datepart,RecordType_Frequency_StartDate,todaydate) + Coefficient>
<!---start date is set to current RecordType_Frequency_StartDate which is a column value from the database--->
<cfset startdate = RecordType_Frequency_StartDate>
<!---sets the next start date for when the for should schedule based on historic db start date--->
<cfset new_date = dateformat(DateADD(datepart,Coefficient,startdate),'MM-DD-YYYY')>
<cfloop from="1" to="#enddate#" index="i">
<cfset new_date = dateformat(DateADD(datepart,Coefficient,startdate),'MM-DD-YYYY')>
<cfset startdate = new_date>
<cfset diff = datediff(datepart,RecordType_Frequency_StartDate,startdate)>
<cfif (startdate GT todaydate)>
<cfset next_date= startdate>
<cfoutput>
<!---I need this output to equal the next date value that would fall based on the schedule, future date. I am seeing multiple dates and need to figure out how to capture would weould truly be the next scheduled date--->
Next Date = #diff# - #dateformat(next_date)#<br />
</cfoutput>
</cfif>
</cfloop>
总之,我的表格是按计划进行的。开始/设置日期是我必须使用的唯一日期。我需要使用我拥有的信息来获取或填充表单的下一个计划日期。显然,下一个创建日期需要在未来,因为此日期将与预定事件一起使用。
我发布了带注释的代码,需要帮助抓住最接近当前日期的下一个逻辑日期,该日期应按顺序排列。
答案 0 :(得分:2)
http://cfquickdocs.com/#DateAdd
如果您只需要下一个可能的日期,请使用dateadd()函数。
例如,如果您希望下一个工作日使用:dateadd("w", 1, now())
答案 1 :(得分:0)
假设我理解你的问题,那么我后来写的一个UDF可能会解决你的问题:
http://www.bryantwebconsulting.com/blog/index.cfm/2011/2/24/EnglinshFriendly-Interval-Calculation
在“interval”参数的其他几个选项中,它也应该接受你在switch块中使用的那些。