Reaction at 00:00, how to do it?

  • 0
    I need to do a certain action EXACTLY at 00:00
    JavaScript Eliana Mora, Jan 29, 2020

  • 3 Answers
  • 0
    function cloneOrCreateDate(date) {
    return date instanceof Date ? new Date(date) : new Date();
    }
    function getMidnightDate(date) {
    const d = cloneOrCreateDate(date);
    d.setHours(0);
    d.setMinutes(0);
    d.setSeconds(0);
    d.setMilliseconds(0);
    return d;
    }

    function addDaysToDate(days, date) {
    const d = cloneOrCreateDate(date);
    d.setDate(d.getDate() + days);
    return d;
    }

    function timeOffset(d1, d2) {
    return Math.abs(d1 - d2);
    }

    setTimeout(() => {
    // логика в полночь
    }, timeOffset(Date.now(), addDaysToDate(1, getMidnightDate())));
    Mila Davidson

  • 0
    Obviously, a certain condition should be checked every second.

    And most likely in setInterval .



    If we talk about simple.



    But you can use a special library , I honestly have not used the scheduler on the node.

    But the principles of planners are similar.
    Anonymous

  • 0
    const getCurrentTime = () => {
    let date = new Date(),
    hours = "",
    minutes = "";
    if (String(date.getHours()).length < 2) {
    hours += String(0) + String(date.getHours())
    } else {
    hours = `${date.getHours()}`
    }
    if (String(date.getMinutes()).length < 2) {
    minutes += String(0) + String(date.getMinutes())
    } else {
    minutes = `${date.getMinutes()}`
    }
    return `${hours}:${minutes}`
    }

    if (getCurrentTime() === '00:00') {
    // действие
    }
    Anonymous

Your Answer
To place the code, please use CodePen or similar tool. Thanks you!