While I was looking through the Haml docs today, I found the ability to print out the date. My gears started spinning. Why not create a calendar?
It took a lot of research, but I made a functioning Haml Calendar. I say functioning, because it will display the correct date and tomorrow it will automatically display tomorrow’s date. You may also change the month, year, and day variables to display a specific date.
I realize this is not a practicular calendar solution, but it was fun to figure out.
The first step: print out today’s date. Once I got that, I created variables for month, year, and day that default to today’s date unless otherwise specified. I also created an array
@days to hold the days of the week in the order I prefer.
I looped through
@days to create the headers for the calendar, but I decided to only display the first letter of each day. I used a list because I find that they are easier to style and they help keep my code organized.
Next I created a new list for the ordinal days of the calendar using a new loop:
I started by printing 31 days (with the start of the month at Sunday for now). I styled the list items so that only 7 can fit in each row. The list items naturally fell into calendar order.
At this point I had a calendar, but it wasn’t accurate. August begins on Thursday, not Sunday. I found that if I adjusted the starting value of my loop, I could get the first of the month to start on Thursday
- (-3...32).each do |i|. That works, except now Sunday reads as -3 and so forth. I added a few constraints:
The items that are
i <= 0 still exist, but they don’t print a number. Instead they create blank space allowing August to begin on Thursday.
The calendar works, but it’s not dynamic.
Making it Dynamic
I wrote a new variable called
monthStart. Using the date function along with my variables for month and year, I could get the day each month starts on. I wrote another variable to find the position of the first of the month in my
@days array. So in
@days Thursday’s position is 4, but my loop needs to start at -3 for the month to start on that day. I wrote down every day, its position in the array, and what number the loop needs to start on in order for the month to start on that day. From these findings, I created a formula. I take the negative of the hash and add 1.
So I updated my loop:
To make sure my loop worked, I changed my
month variable to 9 to display September. It worked!
However, September only has 30 days and I’m still printing out 31.
I wrote another variable called
monthEnd. This new variable was slightly easier than monthStart as I automatically receive an integer.
I updated my loop once more (Here I add 1 to monthEnd to make sure it gets the last day and not up until the last day of the month):
I added a few more constraints when printing out the days to highlight today and to mute the days before today. I think I can write this more efficiently, but I’m ok with my progress so far.
If you edit in CodePen, try writing in a custom date. I commented out the space for you to give it a try.
I really enjoyed figuring this out. I’m new to Ruby, but I think I will do more experimenting!
p.s. I’m not sure about editing the timezone, so it might be a day ahead.