2 Replies Latest reply on Dec 31, 2013 8:31 AM by Winston2

    Highlighting the current day

    Winston2

      Hi, happy new year.

      I have some code that gives me a calendar on my cfml page.

      Is there a way that I can modify this so that the current day has a background color?

      Thank you very much.

       

       

       

      <code>

       

      <cfset thisYear = year(now())>

      <cfset thisMonth = month(now())>

       

       

      <p><cfoutput>#MonthAsString(thisMonth)# of #thisYear#</cfoutput>

       

       

      <cfoutput>

      <table border="1" width="100%" height="100%">

         <tr>

         <cfloop index="x" from="1" to="7">

            <th>#dayOfWeekAsString(x)#</th>

         </cfloop>

         </tr>

      </cfoutput>

       

       

      <cfset firstOfTheMonth = createDate(year(now()), month(now()), 1)>

       

       

      <cfset dow = dayofWeek(firstOfTheMonth)>

      <!---This gives us a number between 1 and 7. Our pad will be the day of the week minus 1.--->

       

       

      <cfset pad = dow - 1>

      <!---Now we need to create the first row of the calendar with empty boxes, if any:--->

       

       

      <cfoutput>

      <tr>

      </cfoutput>

       

       

      <cfif pad gt 0>

         <cfoutput><td colspan="#pad#"> </td></cfoutput>

      </cfif>

       

       

      <cfset days = daysInMonth(now())>

      <cfset counter = pad + 1>

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

         <cfoutput>

         <td>#x#</td>

         </cfoutput>

         <cfset counter = counter + 1>

         <cfif counter is 8>

            <cfoutput></tr></cfoutput>

            <cfif x lt days>

               <cfset counter = 1>

               <cfoutput>

               <tr>

               </cfoutput>

            </cfif>

         </cfif>

      </cfloop>

       

       

      <cfif counter is not 8>

         <cfset endPad = 8 - counter>

         <cfoutput>

         <td colspan="#endPad#"> </td>

         </cfoutput>

      </cfif>

       

      </code>

        • 1. Re: Highlighting the current day
          duncancumming Level 3

          So it looks like you're only outputting the current month, which makes things easier (if you add the ability to navigate backwards or forwards through the months and years, this code would need changed slightly).

           

          Find out what today is:

           

          <cfset thisDay = day(now())>

           

          Then as you're looping over your 28-31 days, check if you're on the current day:

           

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

              <cfif x EQ thisDay>

                  <td style="background-color:red">

              <cfelse>

                  <td>

              </cfif>

             <cfoutput>#x#</cfoutput>

             </td>

          • 2. Re: Highlighting the current day
            Winston2 Level 1

            Thanks. It worked perfectly.

            I appreciate the help.