Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

Highlighting the current day

Explorer ,
Dec 31, 2013 Dec 31, 2013

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>

503
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Engaged , Dec 31, 2013 Dec 31, 2013

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#</c

...
Translate
Engaged ,
Dec 31, 2013 Dec 31, 2013

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>

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Dec 31, 2013 Dec 31, 2013
LATEST

Thanks. It worked perfectly.

I appreciate the help.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources