DEV Community

Cover image for Project Two: Calendar
Valeria
Valeria

Posted on

Project Two: Calendar

Once upon a time we've build a calendar and it was working great! Well, up until the moment a whole month disappeared from it. We had January, February, March, May, July, August, a couple of Septembers... Yeah, it was wild, almost as bad as the AI generated one on this article's poster! The problem was that we used a third party library and despite our best efforts we failed to find the issue causing the bug. I wish I had a ready calendar skeleton back then - would save us a lot of time!

And hence I suggest you build one for yourself. Working with dates is all fun and games till timezones come into play, so be mindful of that whenever you do - and, judging by my own experience, calendars, date inputs, launch dates and schedules - are features I had almost in every project.

Calendar view: requirements & recipe

As a user I want to be able see a calendar for a selected month and year, so that I could see day of the week of the date in question.

Here's an example how this view could like:

Calendar from December 1, 2023 to December 31, 2023 in terminal with December 2 (current date) highlighted

Of course, you're free to implement it in HTML, like normal people, or you could follow my strange desire to make it a terminal view - it's up to you.

However, there are some requirements to keep in mind:

Requirements

  1. Calendar should show week days with the order in accordance with the locale of the user.
  2. All rows should have exactly 7 cells, padded with empty elements or faded out dates from the previous/next month.
  3. February should have a proper amount of days for leap and regular years alike.
  4. Current date should be highlighted.
  5. Think about accessibility: how should screen readers read your calendar?

Recipe

  1. Start with a static markup for the current month and year.
  2. Ensure that markup is accessible and semantic, e.g. with the help of Lighthouse
  3. Write a function that would create a list of dates for a provided month and year. I highly recommend writing some tests, especially around leap year logic.
  4. Write a function that would split days into rows for the order of weekdays you're most comfortable with (e.g. when week starts from Sunday). Ensure that all rows have exactly 7 elements.
  5. Adjust the previous function to work for the other case (e.g. when week starts from Monday).
  6. Create a function that would render the calendar using your favourite framework (or none).
  7. Highlight current date.
  8. Profit.

Hints

Almost any high-level language has a Date abstraction in the standard library: you can use it not only to construct proper dates, but also to verify the ones that aren't.
For example setting a day to 31 in a month that has only 30 days wouldn't work - and that could help you determine how many days a month has. Or setting a date to a 0 would result in the last day of the previous month.

Have fun!

Hard mode: Dates range input

There's a built-in HTML <input type="date" .../>, but unfortunately it doesn't support selection of a. range of dates. Selecting two dates from two inputs is also an option and is great for accessibility, but unfortunately it doesn't always fit into a page design.

Therefore, you could create a text input that would allow user to enter dates as December, 1 - December, 31 2023 or 1.12.2023 - 31.12.2023 and render this selection on a calendar.

A good input should be able to validate the dates, for example that start date is before the end date and that both are valid dates that can be parsed.

Share your creations and feedback and see you tomorrow!

Liked the idea a lot and would love to have it all year long?

Buy Me A Coffee

Top comments (4)

Collapse
 
fyodorio profile image
Fyodor

Share your creations and feedback and see you tomorrow

That’d take a bit more time than the avatar one 😅

Collapse
 
valeriavg profile image
Valeria

Indeed, it might seem this way. Is it the styling part that feels too much or something else?

Collapse
 
fyodorio profile image
Fyodor

No, I’d say the dynamic part is definitely trickier (the inputs and alike), as drawing the calendar grid is more or less trivial by itself

Thread Thread
 
valeriavg profile image
Valeria

I see, you can limit the scope and start with just one format for the input for example. Whatever feels fun and still challenging. Let me know how it goes!