Building Your Own Effort Calendar with React and Day.js

Building Your Own Effort Calendar with React and Day.js

1. Background to Implementing a Manpower Calendar from Scratch

In a web service, a calendar may look like a simple UI for selecting dates, but implementing one requires considering several factors, such as the number of days in each month, weekday alignment, empty date cells, and the placement of information for each date. In particular, unlike a standard calendar that displays only dates, a manpower calendar needs to show work-related information such as manpower and amounts, so we needed a structure that met the service’s requirements.

For this project, I implemented the manpower calendar used in the BanJang Note service from scratch. In addition to displaying basic dates, it can show information for each date. I focused on separating the roles of its components so they could be reused across multiple screens.

One important consideration when implementing the calendar’s basic structure was separating date calculations from their visual presentation. Even if the date calculations are accurate, the calendar cannot be used properly if the dates are not placed under the correct weekdays. Conversely, even with a well-designed screen structure, incorrect date calculations can cause dates to be omitted or displayed in the wrong positions. Therefore, I considered both the logic for calculating dates and the structure for placing the results on screen.

2. React Components and Date Data Structure

First, I divided the responsibilities among CustomCalendar, which manages the entire calendar; CalendarMonthGrid, which displays the dates for one month; and the buildWeeks function, which creates an array of dates.

CustomCalendar manages the calendar’s overall state and screen layout, while CalendarMonthGrid renders the weekdays and dates for the month it receives. buildWeeks calculates the dates for a given month and divides them into week-long groups.

Separating the functionality this way makes it possible to review the date calculation logic and screen structure independently. It reduces the scope of changes needed elsewhere in the UI when modifying how dates are generated, and also limits the impact on date calculations when updating the UI.

I defined the data for each date using the CalendarDayData type.

export type CalendarDayData = {
  effort?: number;
  amount?: number;
  holidayName?: string;
  totalWorkers?: number;
  attendedWorkers?: number;
  memo?: string;
};

I added ? to each property because the information available may differ from one date to another. Some dates may have only a manpower value, while others may have a holiday name or a memo, so the structure allows only the necessary information to be passed in.

Data for each date is managed in an object that uses date strings as keys.

type WorkdaysCalendarProps = {
  value: Dayjs | null;
  onChange: (date: Dayjs | null) => void;
  data?: Record<string, CalendarDayData>;
};

Record<string, CalendarDayData> is a type that associates string keys with objects containing information for each date. Using dates as keys makes it possible to look up the information needed for a given date directly.

const calendarData: Record<string, CalendarDayData> = {
  '2026-10-01': {
    effort: 1,
  },
  '2026-10-02': {
    effort: 0.5,
    memo: '오전 근무',
  },
};

To use this structure, the format of the date keys must be consistent. 2026-10-01 and 2026.10.01 are different strings, so the same format must be used when generating dates and looking up data. In this implementation, I used Day.js’s format() to standardize dates in the YYYY-MM-DD format.

3. Date Calculations with Day.js

A key part of implementing a calendar is accurately calculating the dates for a given month. Since the number of days varies by month and the empty spaces at the beginning depend on the weekday of the first day, simply listing dates from the 1st is not enough to create a calendar layout.

For this project, I used Day.js to simplify date calculations. Day.js is a JavaScript library that provides functions for creating dates, checking the number of days in a month, moving dates, comparing dates, and converting them to strings.

First, I created the current date with dayjs() and used startOf() to get the beginning of a specific unit of time.

const [visibleMonth, setVisibleMonth] = useState(value ?? dayjs());

const next = visibleMonth
  .add(delta, 'month')
  .startOf('month');

visibleMonth represents the month currently being displayed. If a date is passed in from the parent component, the calendar starts with that date; otherwise, it uses the current date.

When moving between months, I add the number of months using add() and then use startOf('month') to set the date to the first day of that month. For example, adding one month to October 16, 2026 gives November 16, but applying startOf('month') sets the date to November 1. Since there is no need to retain the day when managing dates by month, this makes the basis for changing months clear.

daysInMonth() returns the number of days in the given month. This eliminates the need to handle the number of days in each month using conditional statements.

for (let day = 1; day <= month.daysInMonth(); day += 1) {
  days.push(month.startOf('month').date(day));
}

The loop runs from the 1st through the last day of the month. date(day) is a method that sets the day of the month, creating each date relative to the first day of the month. Even when the number of days varies by year, as in February, daysInMonth() returns the actual number of days, so the same logic can handle it.

I use day() to determine the weekday. This method represents Sunday as 0, Monday as 1, and Saturday as 6. Checking the weekday of the first day of the month lets us calculate how many empty spaces are needed before the dates.

format(), which converts dates to strings, also plays an important role.

const dateKey = date.format('YYYY-MM-DD');
const monthTitle = visibleMonth.format('YYYY년 M월');

The first code generates a key for retrieving date-specific data, and the second generates the month title to display on the screen. MM always represents the month using two digits, while M represents single-digit months without a leading zero. Therefore, YYYY-MM-DD, which has a consistent format, is used for the data key, and the more readable YYYY year M month format is used for the screen title.

I used isSame() to compare dates.

const isSelected = value?.isSame(date, 'day');
const isToday = date.isSame(dayjs(), 'day');

Passing 'day' as the second argument to isSame() compares the dates themselves, rather than the times. To check whether two dates are in the same month, you can specify 'month'. Setting the comparison unit according to the purpose makes it possible to handle dates and months separately.

Another feature of Day.js is its immutability. add() and startOf() return the result of the calculation without directly modifying the original date object. This makes it possible to preserve the original date while managing the updated date separately. It helps prevent the original value from being changed unexpectedly during date calculations and makes it easier to manage the flow of changes in React state.

4. Organizing Dates into Weeks

After calculating the dates, we need to arrange them in an array so they can be displayed in an actual calendar layout. In this implementation, I used the buildWeeks function to create an array of dates and divide it into groups of seven elements, one for each week.

const buildWeeks = (month: Dayjs) => {
  const days: (Dayjs | null)[] = Array.from(
    { length: month.startOf('month').day() },
    () => null,
  );

  for (let day = 1; day <= month.daysInMonth(); day += 1) {
    days.push(month.startOf('month').date(day));
  }

  return Array.from(
    { length: Math.ceil(days.length / 7) },
    (_, index) => days.slice(index * 7, index * 7 + 7),
  );
};

In the first step, I create the empty spaces needed before the first day of the month. I get the weekday of the first day using startOf('month').day() and add that number of null values to the array. If the first day is Sunday, no empty spaces are needed; if it is Wednesday, three spaces are needed at the beginning.

Here, I used Array.from() to create an array of the specified length. Positions without dates are represented by null, and the array’s type is defined as (Dayjs | null)[] to indicate that it can contain both actual dates and empty spaces.

In the second step, I add dates in a loop for the number of days returned by daysInMonth(). Finally, I divide the length of the entire array by 7 and apply Math.ceil() to calculate the number of weeks needed. I used slice() to divide the array into groups of seven.

This process transforms a flat array of dates into an array grouped by week. On screen, an outer loop can create each week, and an inner loop can render each date cell.

However, the current implementation adds the empty spaces needed before the first day of the month, but does not separately create empty spaces to fill the remaining cells in the final week. As a result, the final week’s array may contain fewer than seven elements. If every row needs to have the same structure, you can add empty spaces to the end of the date array or use the CSS layout to keep the row and column sizes consistent.

As this illustrates, the way the date array is generated also affects the alignment on screen. When a calendar alignment issue occurs, it is important to check not only the CSS but also whether the empty spaces are correctly included in the date array and how many elements each week contains.

5. Calendar Layout with CSS Grid

After calculating the dates, we need to display the resulting data in an actual calendar layout. In this implementation, I separated the weekday section from the date section and arranged each week as a separate row.

<div className="custom-calendar-weekdays">
  {WEEKDAYS.map((weekday) => (
    <div key={weekday} className="custom-calendar-weekday">
      {weekday}
    </div>
  ))}
</div>

<div className="custom-calendar-grid">
  {buildWeeks(month).map((week, weekIdx) => (
    <div className="custom-calendar-week" key={weekIdx}>
      {week.map((date, idx) => {
        if (!date) {
          return (
            <div
              key={`empty-${weekIdx}-${idx}`}
              className="custom-calendar-day empty"
            />
          );
        }

        return (
          <div
            key={date.format('YYYY-MM-DD')}
            className="custom-calendar-day"
          >
            <div className="custom-calendar-day-number">
              {date.date()}
            </div>
          </div>
        );
      })}
    </div>
  ))}
</div>

The weekdays are rendered by iterating over the WEEKDAYS array, and the date section is rendered by iterating over the week-based array returned by buildWeeks(). Matching the structure of the data array to the structure of the UI makes it easy to see how the date calculation results are displayed.

It is important to render empty spaces as separate elements as well. If elements without dates are simply skipped, the actual dates may shift forward and appear under the wrong weekdays. Therefore, empty spaces should follow the same layout flow as date cells so that dates appear in the correct positions.

In a calendar built with CSS Grid, it is important to keep the seven columns consistent. The width of each date cell should not change even when the amount of information varies, and the positions of the date numbers and manpower information should also remain consistent.

Borders also need to be considered. Applying borders on all sides of every adjacent date cell can cause the edges to overlap and look thicker. On the other hand, removing some borders to fix this may leave lines missing at the last row or column. Therefore, decide whether the individual cells or the overall area are responsible for the borders, and ensure that all elements, including empty spaces, follow the same rules.

The final row of the calendar can also vary depending on the structure of the date array. As explained above, if empty spaces are not added to the last week, the appearance of that row will depend on how CSS lays it out. Therefore, creating the desired layout requires reviewing not only the CSS but also how the date data is generated.

This implementation showed that it is difficult to treat data generation and screen layout as separate concerns in a calendar UI. A stable calendar layout requires considering together whether the date calculations are accurate, whether empty spaces are represented correctly, and whether each row and column follows consistent rules.

6. Lessons from the Implementation

By implementing a manpower calendar from scratch, I learned that a calendar is not simply a UI that lists dates. It is a feature that requires date calculations, data structures, component design, and CSS layout to work together.

Day.js makes it possible to clearly express the process of finding the start of a month and the number of days in it, checking weekdays, creating dates, and converting them into the required format. In particular, using startOf(), daysInMonth(), day(), date(), format(), add(), and isSame() for their respective purposes helped reduce the conditional logic needed for date calculations and clarify the intent of the code.

Separating the date calculation logic into its own function and connecting it to the components that build the UI is also valuable from a maintenance perspective. Separating date generation from visual presentation makes it possible to review each part independently and makes it easier to identify the scope of changes when adding features later.

Most importantly, I learned that the structure of the date array directly affects screen alignment. For dates to appear under the correct weekdays, the empty spaces before the first day must be calculated accurately, and the shape of the final row must also be taken into account. When resolving an issue on screen, it is necessary to check how the data is generated rather than only changing the CSS.

This project was an opportunity to understand not only how to build a screen to match a design, but also the calculation principles and data structures required for the UI to work accurately. I learned that writing stable, maintainable code requires more than just using library methods: it is important to understand what values each method returns and why it is needed in the implementation.

KKAMJJING

Site footer