how to add time to a timetable in javascript

To add time to a timetable in JavaScript, you can use the Moment.js library which makes working with dates and times much easier.

Here's an example code snippet that demonstrates how to add time to a timetable using Moment.js:

index.tsx
// Import the moment.js library
const moment = require('moment');

// Create a new timetable object
const timetable = {
  monday: {
    start: '9:00 AM',
    end: '5:00 PM'
  },
  tuesday: {
    start: '9:00 AM',
    end: '5:00 PM'
  },
  // ...
};

// Add 30 minutes to the start time of Monday's timetable
timetable.monday.start = moment(timetable.monday.start, 'h:mm A').add(30, 'minutes').format('h:mm A');

// Add 1 hour to the end time of Tuesday's timetable
timetable.tuesday.end = moment(timetable.tuesday.end, 'h:mm A').add(1, 'hour').format('h:mm A');

console.log(timetable);
587 chars
24 lines

In this example, we first create a timetable object which contains start and end times for each day of the week. We then use the moment() function to convert the start and end times from a string format to a moment object which we can manipulate using the add() function. Finally, we use the format() function to convert the moment object back to a string in the desired format.

gistlibby LogSnag