While playing around at lunchtime today, I decided to write a quick little HTML calendar generator.
I consider C# my native programming language, and since I intended to output HTML, ASP.NET seemed a logical choice. But I was amazed at the amount of code required for such a seemingly simple task (not to mention how ugly code containing <% and %> is!).
The challenge
Using your favorite programming language / web application framework output the following html fragment:
<table class="calendar"> <thead> <tr><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th></tr> </thead> <tbody> <tr><td class="prevMonth">30</td><td class="prevMonth">31</td><td>1</td><td>2</td><td>3</td><td>4</td><td>5</td></tr> <tr><td>6</td><td>7</td><td class="today">8</td><td>9</td><td>10</td><td>11</td><td>12</td></tr> <tr><td>13</td><td>14</td><td>15</td><td>16</td><td>17</td><td>18</td><td>19</td></tr> <tr><td>20</td><td>21</td><td>22</td><td>23</td><td>24</td><td>25</td><td>26</td></tr> <tr><td>27</td><td>28</td><td>29</td><td>30</td><td class="nextMonth">1</td><td class="nextMonth">2</td><td class="nextMonth">3</td></tr> </tbody> </table>
Whitespace is insignificant; class="today" should be applied to the current day’s cell; favor simplicity and elegance over efficiency.
Blog your solution and leave a comment; I’ll link to it from this post.
Please don’t comment suggesting I use some pre-built control. This is about the fun of writing such a thing, not about some specific calendaring need.
To avoid influencing your initial attempt, I’ll post my solution after awhile.
UPDATE:
Will Asrari contributed a submission at http://www.willasrari.com/blog/coding-for-fun—html-calendar-challenge/000298.aspx.
I posted my solution at http://jacobcarpenter.wordpress.com/2008/04/16/pc1-a-solution/.
Writing code for fun? People do that?
Comment by sheen — April 8, 2008 @ 6:07 pm
The ASP.NET Table control is your best friend for something like this.
Comment by John S. — April 8, 2008 @ 6:57 pm
Ah, sure; you could do an ASP.NET table-control-based solution. I just meant I wasn’t interested in someone touting the virtues of their favorite prebuilt all-in-one date-picker/calendar control.
Comment by Jacob — April 8, 2008 @ 9:12 pm
I’m in. Coding for fun is right up my alley.
Not too long ago I found myself scratching my head when trying to determine if 2 date ranges intersected. The solution ended up being a one-liner but the road to that solution was quite interesting.
It’s funny how easy it is to underestimate the difficulty of seemingly trivial tasks.
Comment by Will Asrari — April 15, 2008 @ 11:19 am
I’d like to submit my entry:
http://www.willasrari.com/blog/coding-for-fun—html-calendar-challenge/000298.aspx
Comment by Will Asrari — April 15, 2008 @ 12:04 pm
[...] challenge, csharp, extension methods — Jacob @ 12:21 pm So, when I initially posed the programming challenge #1 I stated: … since I intended to output HTML, ASP.NET seemed a logical choice. But I was [...]
Pingback by PC#1: A solution « Jacob Carpenter’s Weblog — April 16, 2008 @ 12:21 pm