Skip to main content
Inspiring
November 20, 2009
Answered

Advance date 90 days when date change in Form

  • November 20, 2009
  • 1 reply
  • 2130 views

Good evening all,

I have this:

<cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#">

<cfinput type="datefield" name="nextdate" value="#dateformat(Now() +90, 'mm/dd/yyyy')#" readonly="yes">

I want to advance "nextdate" by 90 days  when "startdate" is changed.

This topic has been closed for replies.
Correct answer BKBK

Use cfinput's bind attribute, for example


<script type="text/javascript">

/* add 90 days to a given date */
addDays = function(dateStr) {
var newdate = (new Date(dateStr)).add('d',90);
var days = newdate.getDate();
var months = newdate.getMonth()+1;
var year = newdate.getFullYear();

/* the datefield widget requires 2 digits to display*/
if(days < 10) days = '0'+days;
if(months < 10) months = '0'+months;
return months + "/" + days + "/" + year;
}
</script>

<cfform name="dateForm" id="dateForm">
<cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#">
<cfinput type="datefield" name="nextdate" bind="javascript:addDays({dateForm:startdate})" readonly="yes"> 
</cfform>

1 reply

BKBK
Community Expert
BKBKCommunity ExpertCorrect answer
Community Expert
November 22, 2009

Use cfinput's bind attribute, for example


<script type="text/javascript">

/* add 90 days to a given date */
addDays = function(dateStr) {
var newdate = (new Date(dateStr)).add('d',90);
var days = newdate.getDate();
var months = newdate.getMonth()+1;
var year = newdate.getFullYear();

/* the datefield widget requires 2 digits to display*/
if(days < 10) days = '0'+days;
if(months < 10) months = '0'+months;
return months + "/" + days + "/" + year;
}
</script>

<cfform name="dateForm" id="dateForm">
<cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#">
<cfinput type="datefield" name="nextdate" bind="javascript:addDays({dateForm:startdate})" readonly="yes"> 
</cfform>

djkhalifAuthor
Inspiring
November 23, 2009

BKBK,

Thanks. This is what I was looking for. I made a slight change. I changed the second <cfinput type="text">, I left it readonly so that users cannot change the 90 day date.

<cfform name="dateForm" id="dateForm">
<cfinput type="datefield" name="startdate" value="#dateformat(Now(), 'mm/dd/yyyy')#">
<cfinput type="text" name="nextdate" bind="javascript:addDays({dateForm:startdate})" readonly="yes"> 
</cfform>

Thanks again,

djkhalif

djkhalifAuthor
Inspiring
November 23, 2009

BKBK,

I couldn't get it to work with flash format.

THanks,

djkhalif