KhTech - Заметки

Рабочие заметки и инструкции

View the Project on GitHub

Задача: Менять дату и время на Tilda по расписанию. Вебинар будет проводиться каждый час с 10am до 7pm. При входе на страницу пользователь должен видеть дату и время в зависимости от текущего времени.

/**
 * Panel for setting the conditions of the time change for the webinar.
 */
const webinarSchedule = [
  { startHour: 8, startMinute: 45, endHour: 9, endMinute: 45, webinarTime: '10:00' },
  { startHour: 9, startMinute: 45, endHour: 10, endMinute: 45, webinarTime: '11:00' },
  { startHour: 10, startMinute: 45, endHour: 11, endMinute: 45, webinarTime: '12:00' },
  { startHour: 11, startMinute: 45, endHour: 12, endMinute: 45, webinarTime: '13:00' },
  { startHour: 12, startMinute: 45, endHour: 13, endMinute: 45, webinarTime: '14:00' },
  { startHour: 13, startMinute: 45, endHour: 14, endMinute: 45, webinarTime: '15:00' },
  { startHour: 14, startMinute: 45, endHour: 15, endMinute: 45, webinarTime: '16:00' },
  { startHour: 15, startMinute: 45, endHour: 16, endMinute: 45, webinarTime: '17:00' },
  { startHour: 16, startMinute: 45, endHour: 17, endMinute: 45, webinarTime: '18:00' },
  { startHour: 17, startMinute: 45, endHour: 19, endMinute: 00, webinarTime: '19:00' },
  // Default time if none of the above
  { startHour: 19, startMinute: 00, endHour: 24, endMinute: 0, webinarTime: '19:00' },
  { startHour: 0, startMinute: 0, endHour: 8, endMinute: 45, webinarTime: '19:00' }
];

/**
 * Get the name of the month.
 * @param {Date} date - The date object from which to extract the month.
 * @returns {string} - Returns the name of the month in Russian.
 */
function getMonth(date) {
    let months = ['января', 'февраля', 'марта', 'апреля', 'мая', 'июня', 
                 'июля', 'августа', 'сентября', 'октября', 'ноября', 'декабря'];
    return months[date.getMonth()];
}

/**
 * Get the day of the month, incrementing by one if after a certain time.
 * @param {Date} date - The date object from which to extract the day.
 * @returns {number} - Returns the day of the month, incremented if needed.
 */
function getDay(date) {
    let currentHour = date.getHours();
    if (currentHour >= 18 && currentHour < 24) {
        date.setDate(date.getDate() + 1);
    }
    return date.getDate();
}

/**
 * Get the webinar time based on the current time.
 * @param {Date} date - The current date and time.
 * @returns {string} - Returns the webinar time in 'HH:MM' format.
 */
function getWebinarTime(date) {
    let currentHour = date.getHours();
    let currentMinute = date.getMinutes();
    for (let i = 0; i < webinarSchedule.length; i++) {
        let slot = webinarSchedule[i];
        if ((currentHour === slot.startHour && currentMinute >= slot.startMinute) || 
            (currentHour === slot.endHour && currentMinute < slot.endMinute) || 
            (currentHour > slot.startHour && currentHour < slot.endHour)) {
            return slot.webinarTime;
        }
    }
    // If none of the time slots match, return the default fixed time
    return '19:00';
}

/**
 * Update the date and time in the page based on the current time and the webinar schedule.
 */
function updateDateTime() {
    let dateElement = document.querySelector('.dateClass > .tn-atom');
    let timeElement = document.querySelector('.timeClass > .tn-atom');
    
    let currentDate = new Date();
    
    dateElement.textContent = `${getDay(currentDate)} ${getMonth(currentDate)}`;
    timeElement.textContent = `${getWebinarTime(currentDate)} по мск`;
}

// Call the function to update the date and time on page load
updateDateTime();