how to use moment to compare time for calendar change color 1

how to use moment to compare time for calendar change color

var checkTime = function () {
    var hour = $(".hour").text().trim();

    var time = moment(hour, "LT");
    console.log(time)

    //remove any old classes from element
    $(".hour").removeClass(".present .past .future");

    // apply new class if task is near/over due date
    if (moment().isAfter(time)) {
        $(".hour").addClass(".past");
    } else if (moment().isBefore(time)) {
        $(".hour").addClass(".future");
    } else {
        $(".hour").addClass(".present");
    }
}

checkTime();

Here is what the above code is Doing:
1. We’re creating a function called checkTime.
2. We’re creating a variable called hour and setting it equal to the text of the hour div.
3. We’re creating a variable called time and setting it equal to the moment of the hour variable.
4. We’re removing any old classes from the hour div.
5. We’re adding a class of past if the current time is after the hour.
6. We’re adding a class of future if the current time is before the hour.
7. We’re adding a class of present if the current time is equal to the hour.
8. We’re calling the checkTime function.

Similar Posts