Skip to main content
Participant
February 16, 2012
Answered

Edit Selectable Dates in DateField

  • February 16, 2012
  • 2 replies
  • 833 views

im trying to create an event form where the datefield will only allow a date from a specific amount of dates to be selected from.

For example, something like this: if the date (today) is greater than the monday of the 4th week of  the month, (so for this month, February, it would have to be > 20th) then this month (would be nice if it also eliminated the previous days that already passed in the month) and next month is has available dates to select. Else (if less than the 4th week of the month, <20 for february) only this month(february) is has available dates to be picked from.

Just wondering how can you do this with a <cfinput type="datefield> tag. I have come up with something but am not sure how to go from here.

//Cfset's

<cfset theDate = Now()>

<cfset NextMonth = DateAdd('m', 1, theDate) />

<cfset nextMonthNum = daysInMonth(#NextMonth#) />

<cfset weekNum = (#weekOfMonth(now())#) />

<cfset daysNum = daysInMonth(now()) />

<cfset dateToday = Now()>

//Datefield

<cfinput

     type="datefield"

     label="date"

     mask="mm/d/yy"

     name="activitydate"

     value="#dateAdd('d', +1,now())#"

     required="yes"

     message="Please Select a Valid Date for Event" />

//This is where it checks for the amount of days and loops them through giving the range of available dates from the begining of the month.

<cfscript>

                                                  function weekOfMonth(thisDate) {

                                                            var thisDay=day(thisDate);

                                                            var thisWeek=0;

                                                            if (thisDay LTE 7)

                                                            thisWeek=1;

                                                            else if (thisDay GT 7 AND thisDay LTE 14)

                                                            thisWeek=2;

                                                            else if (thisDay GT 14 AND thisDay LTE 21)

                                                            thisWeek=3;

                                                            else

                                                            thisWeek=4;

                                                            return thisWeek;

                                                  }

                                        </cfscript>

<cfif #DatePart('w', TheDate)# GTE 4 AND #weekNum# GTE 2>

                    True<br />

                    <cfloop index="x" from="1" to="#daysNum#">

                                   <cfoutput>

                                            #DateFormat(now(), "mm")#/#x#/#DateFormat(now(), "yy")# <br />

                             </cfoutput>

                                                                 </cfloop>

                    <cfloop index="y" from="1" to="#nextMonthNum#">

                                   <cfoutput>

                                            #DateFormat(NextMonth, "mm")#/#y#/#DateFormat(NextMonth, "yy")#<br />

                             </cfoutput>

                    </cfloop>

                    <cfelse>

                    False<br />

                    <cfloop index="z" from="1" to="#daysNum#">

                                   <cfoutput>#DateFormat(now(), "mm")#/#z#/#DateFormat(now(), "yy")# <br /></cfoutput>

                    </cfloop>

                        </cfif>

Any help greatly appreciated!

    This topic has been closed for replies.
    Correct answer BKBK

    BobbyWales21 wrote:

    im trying to create an event form where the datefield will only allow a date from a specific amount of dates to be selected from.

    For example, something like this: if the date (today) is greater than the monday of the 4th week of  the month, (so for this month, February, it would have to be > 20th) then this month (would be nice if it also eliminated the previous days that already passed in the month) and next month is has available dates to select. Else (if less than the 4th week of the month, <20 for february) only this month(february) is has available dates to be picked from.

    I found this a challenging requirement to express in code. I chose to do it the simple way, with cfselect. With cfselect it is possible for the user to pick a date from a list covering 2 consecutive months.

    <cfset currentYear = year(now())>

    <cfset currentMonth = month(now())>

    <cfloop from="21" to="27" index="d">

        <cfset dateIn4thWeekOfCurrentMonth = createdate(currentYear,currentMonth,d)>

        <cfset dayOfWeekIn4thWeekOfCurrentMonth = dayOfWeek(dateIn4thWeekOfCurrentMonth)>

        <cfif dayOfWeekIn4thWeekOfCurrentMonth EQ 2>

            <cfset mondayDateIn4thWeekOfCurrentMonth = dateIn4thWeekOfCurrentMonth>

            <cfbreak>

        </cfif>

    </cfloop>

    <!---Compare today date with that Monday date and create select-list accordingly --->   

    <cfset todayDate = now()>

    <cfform>

    <cfselect name="chosenDate">

    <option value="">Select a date</option>

    <cfif todayDate LT mondayDateIn4thWeekOfCurrentMonth>

        <!--- Today date is before Monday of 4th week --->

        <cfloop from="1" to="#daysInMonth(now())#" index="d">

          <cfset dateValue = createdate(currentYear,currentMonth,d)>

          <cfoutput><option value="#currentYear#_#currentMonth#_#d#">#dateformat(dateValue, "ddd, d mmm yyyy")#</option></cfoutput>

        </cfloop>

    <cfelse>

        <!--- Today date is on or after Monday of 4th week --->

        <cfset dynamicDateValue = createdate(year(now()),month(now()),day(now()))>

        <cfset thisTimeNextMonth = dateAdd("m",1,now())>

        <cfset nextMonth = month(thisTimeNextMonth)>

        <cfset theYearNextMonth = year(thisTimeNextMonth)>

        <cfset numberOfDaysNextMonth = daysInMonth(thisTimeNextMonth)>

        <cfset lastDayNextMonth = createdate(theYearNextMonth,nextMonth,numberOfDaysNextMonth)>

        <cfloop condition="dynamicDateValue LTE lastDayNextMonth">

            <cfset dayValue = day(dynamicDateValue)>

            <cfset monthValue = month(dynamicDateValue)>

            <cfset yearValue = year(dynamicDateValue)>

              <cfoutput><option value="#yearValue#_#monthValue#_#dayValue#">#dateformat(dynamicDateValue, "ddd, dd mmm yyyy")#</option></cfoutput>   

            <cfset dynamicDateValue = dateAdd("d",1,dynamicDateValue)>

        </cfloop>    

    </cfif>

    </cfselect>

    </cfform>

    2 replies

    BKBK
    Community Expert
    BKBKCommunity ExpertCorrect answer
    Community Expert
    February 17, 2012

    BobbyWales21 wrote:

    im trying to create an event form where the datefield will only allow a date from a specific amount of dates to be selected from.

    For example, something like this: if the date (today) is greater than the monday of the 4th week of  the month, (so for this month, February, it would have to be > 20th) then this month (would be nice if it also eliminated the previous days that already passed in the month) and next month is has available dates to select. Else (if less than the 4th week of the month, <20 for february) only this month(february) is has available dates to be picked from.

    I found this a challenging requirement to express in code. I chose to do it the simple way, with cfselect. With cfselect it is possible for the user to pick a date from a list covering 2 consecutive months.

    <cfset currentYear = year(now())>

    <cfset currentMonth = month(now())>

    <cfloop from="21" to="27" index="d">

        <cfset dateIn4thWeekOfCurrentMonth = createdate(currentYear,currentMonth,d)>

        <cfset dayOfWeekIn4thWeekOfCurrentMonth = dayOfWeek(dateIn4thWeekOfCurrentMonth)>

        <cfif dayOfWeekIn4thWeekOfCurrentMonth EQ 2>

            <cfset mondayDateIn4thWeekOfCurrentMonth = dateIn4thWeekOfCurrentMonth>

            <cfbreak>

        </cfif>

    </cfloop>

    <!---Compare today date with that Monday date and create select-list accordingly --->   

    <cfset todayDate = now()>

    <cfform>

    <cfselect name="chosenDate">

    <option value="">Select a date</option>

    <cfif todayDate LT mondayDateIn4thWeekOfCurrentMonth>

        <!--- Today date is before Monday of 4th week --->

        <cfloop from="1" to="#daysInMonth(now())#" index="d">

          <cfset dateValue = createdate(currentYear,currentMonth,d)>

          <cfoutput><option value="#currentYear#_#currentMonth#_#d#">#dateformat(dateValue, "ddd, d mmm yyyy")#</option></cfoutput>

        </cfloop>

    <cfelse>

        <!--- Today date is on or after Monday of 4th week --->

        <cfset dynamicDateValue = createdate(year(now()),month(now()),day(now()))>

        <cfset thisTimeNextMonth = dateAdd("m",1,now())>

        <cfset nextMonth = month(thisTimeNextMonth)>

        <cfset theYearNextMonth = year(thisTimeNextMonth)>

        <cfset numberOfDaysNextMonth = daysInMonth(thisTimeNextMonth)>

        <cfset lastDayNextMonth = createdate(theYearNextMonth,nextMonth,numberOfDaysNextMonth)>

        <cfloop condition="dynamicDateValue LTE lastDayNextMonth">

            <cfset dayValue = day(dynamicDateValue)>

            <cfset monthValue = month(dynamicDateValue)>

            <cfset yearValue = year(dynamicDateValue)>

              <cfoutput><option value="#yearValue#_#monthValue#_#dayValue#">#dateformat(dynamicDateValue, "ddd, dd mmm yyyy")#</option></cfoutput>   

            <cfset dynamicDateValue = dateAdd("d",1,dynamicDateValue)>

        </cfloop>    

    </cfif>

    </cfselect>

    </cfform>

    Inspiring
    February 16, 2012

    This seems to be more complicated than necessary.  You have this:

    <cfset weekNum = (#weekOfMonth(now())#) />

    and there is another function DayOfWeek(date) that returns a number between 1 and 7.  Sunday is 1.

    So if your weeknum is greater than 3 and your DayofWeek function result is greater than 1 you are showing one set of dates, otherwise you are showing another.  All you have to do is determine what those dates should be.

    I've never actually tried it, but you might be able to use cfinput's range attribute for this.