2012-06-14 79 views
2

我不好意思地承認,我不是最偉大的,當涉及到日期和日期邏輯的ColdFusion。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> 

總而言之,我有一個計劃表。開始/設置日期是我必須使用的唯一日期。我需要使用我擁有的信息來獲取或填充表單的下一個預定日期。顯然,接下來的創建日期,需要在未來的下降,因爲這個日期將在配合使用調度事件。

我已經發布帶有註釋的代碼和需要幫助抓住最接近的下一個邏輯日期本應屬於序列中的當前日期。

+5

我討厭這樣說,但這可能不夠清楚,無法爲您得到答案......也許您可以將其削減爲具有實際值的單個用例...顯示答案*應該是什麼。我有點迷失在上面的代碼 - 不知道是什麼值應該是 - 你知道我是什麼意思? –

+0

看來你真正需要的唯一線是'new_date = dateformat(DateADD(datepart,Coefficient,startdate),'MM-DD-YYYY')' - 我不太確定剩下的代碼的目的是什麼(除了開關)。我同意馬克 - 也許你可以張貼一些真正的價值,並嘗試來形容你要完成好一點什麼。 –

回答

2

http://cfquickdocs.com/#DateAdd

如果你需要的是下一個可能的日期,使用DateAdd()函數。

例如,如果你想下一個工作日使用:dateadd("w", 1, now())