View Full Version : ColdFusion Date Logic Assistance Needed

06-14-2012, 02:14 AM
I am embarrassed to admit that I am not the greatest when it comes to dates and date logic ColdFusion.

I<!---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 value="Weekly">

<CFSET datepart = "ww">


<CFCASE value="Monthly">

<CFSET datepart = "m">


<CFCASE value="Quarterly">

<CFSET datepart = "q">


<CFCASE value="Yearly">

<CFSET datepart = "yyyy">



<!---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>


<!---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 />




In summary, I have forms that are on a schedule. The start/set up date is the only date that I have to use. I need to to grab or populate the next scheduled date for the form using what information I have. Obviously the next creation date needs to fall in the future as this date will be used in conjunction with a scheduled event.

I have posted the code with comments and need help grabbing the next logical date closest to the current date that should fall in sequence.

Any help would be very much appreciated.


07-03-2012, 04:49 PM
You are not using a mask in your DateFormat. Is next_date actually returning as a valid date?