Сравнение времени и изменение цвета календаря с помощью Moment.js: методы и примеры кода

Чтобы сравнить время и изменить цвет в календаре с помощью Moment.js, вы можете использовать несколько методов. Вот несколько примеров кода:

Метод 1: использование Moment.js для сравнения времени

// Assuming you have two time values in variables startTime and endTime
var startTime = moment("2024-01-13 09:00", "YYYY-MM-DD HH:mm");
var endTime = moment("2024-01-13 12:00", "YYYY-MM-DD HH:mm");
if (moment().isBetween(startTime, endTime)) {
  // Change the color for the calendar event
  // Example: Set the background color of the event element to red
  document.getElementById("calendar-event").style.backgroundColor = "red";
}

Метод 2: сравнение времени без Moment.js

// Assuming you have two time values in variables startTime and endTime
var startTime = new Date("2024-01-13T09:00:00");
var endTime = new Date("2024-01-13T12:00:00");
var currentTime = new Date();
if (currentTime >= startTime && currentTime <= endTime) {
  // Change the color for the calendar event
  // Example: Set the background color of the event element to red
  document.getElementById("calendar-event").style.backgroundColor = "red";
}

Метод 3. Использование Moment.js для форматирования и сравнения времени

// Assuming you have a time value in a variable eventTime
// And you want to compare it with the current time
var eventTime = moment("2024-01-13 15:00", "YYYY-MM-DD HH:mm");
var currentTime = moment();
if (currentTime.isSame(eventTime, 'day')) {
  // Change the color for the calendar event
  // Example: Set the background color of the event element to red
  document.getElementById("calendar-event").style.backgroundColor = "red";
}