[removed] IF time >= 9:30 then

前端 未结 8 2087
一整个雨季
一整个雨季 2020-12-30 11:25

I\'m trying to write a statement that says \"if time is this and less than that then\". I can use get hours and get min. However, I\'m having problems combining a time suc

8条回答
  •  不思量自难忘°
    2020-12-30 11:53

    I made this solution simple and easy to read (thus easy to adjust) by using a simple trick.

    The trick is to transform the hour and minutes into a number. Eg 09:30 into 9.30

    var now     = new Date();
    var hour    = now.getHours();
    var minutes = now.getMinutes();
    
    // make the current time of day computable and readable at the same time
    // 9:30 becomes 9.30
    var timeOfDay = hour + (minutes / 100);
    
    console.log('timeOfDay: ' + timeOfDay);
    
    // For setting the time limits simply use the time with a dot instead of a colon
    if( (9.30 <= timeOfDay) && (timeOfDay <= 11.30) ){
        console.log('Time is between 9:30 and 11:30');
    }
    

提交回复
热议问题