create a timetable in javascript

To create a timetable in JavaScript, you can use a combination of HTML, CSS, and JavaScript libraries like date-fns or Moment.js.

Here is an example of a timetable that displays events for a day in HTML:

<div id="timetable"></div>
27 chars
2 lines

You can use CSS to create a table layout for the timetable. For example:

#timetable {
  display: table;
}

#timetable > div {
  display: table-row;
}

#timetable > div > div {
  display: table-cell;
  border: 1px solid black;
}
155 chars
13 lines

Then, in JavaScript, you can use the chosen library to calculate the start and end times of the events and populate the HTML elements with the event details. Here's an example using Moment.js:

index.tsx
// Get the timetable element
const timetable = document.getElementById('timetable');

// Set the start and end times of the day
const startTime = moment().startOf('day').add(8, 'hours');
const endTime = moment().startOf('day').add(18, 'hours');

// Calculate the duration of each event
const eventDuration = 30; // in minutes

// Loop through the hour-long slots and add events where needed
for (let time = moment(startTime); time.isBefore(endTime); time.add(eventDuration, 'minutes')) {
  const eventElement = document.createElement('div');
  eventElement.textContent = 'Event Name';
  eventElement.style.backgroundColor = 'lightblue';
  
  // Add the event to the appropriate time slot
  const timeSlotElement = document.createElement('div');
  timeSlotElement.appendChild(eventElement);
  timetable.appendChild(timeSlotElement);
}
834 chars
22 lines

This code creates a timetable with 30-minute slots from 8am to 6pm and adds an event element to each slot with the text "Event Name" and a light blue background color. Of course, you can customize this code to fit your specific needs.

gistlibby LogSnag