Compare system time with set time in a variable

2

How do I compare the system time with a time set in a variable in jQuery or Javascript?

    
asked by anonymous 25.11.2016 / 17:44

1 answer

3

Suggestion:

function compararHora(str) {
    str = str.split(':');
    var agora = new Date();
    var varData = new Date();
    ['setHours', 'setMinutes', 'setSeconds'].forEach(function(fn, i) {
        return varData[fn](str[i]);
    });
    if (agora == varData) return 0;
    else return agora > varData ? 1 : -1;
}



console.log(compararHora('05:50:00'));
console.log(compararHora('23:50:00'));

Based on the format you indicated for hh:mm:ss , you can break that string with : and use the methods that JavaScript gives us to change the time to a date. Then compares if the time is the same, "greater than" or "less than", and in the function I suggest returns 0 , 1 or -1 ;

    
25.11.2016 / 17:57