Search code examples
javascriptgetdate

var d = new Date (); need the days monday to friday


I need to add the days Monday do Friday to a service button, so the button should be shown from Monday to Friday from 9:00 - 12:00 and Monday to Friday from 13:00 to 16:00 Can somebody help me please :-) Thanks a lot!!

var d = new Date();
if(d.getHours() >= 9 && d.getHours() <= 12 || d.getHours() >= 13 && d.getHours() <= 16){
    $(".servicebutton").show();
}
else {  
    $(".servicebutton").hide();
}

Solution

  • You could do something like:

    let d = new Date();
    let isCorrectHour = (d.getHours() >= 9 && d.getHours() <= 12) || (d.getHours() >= 13 && d.getHours() <= 16);
    let isCorrectDay = d.getDay() >= 1 && d.getDay() <= 5; 
    
    if(isCorrectHour && isCorrectDay) $(".servicebutton").show();
    else $(".servicebutton").hide();
    

    isCorrectHour is the condition on hours (from 9:00 - 12:00 or from 13:00 to 16:00).

    isCorrectDay is the condition on days (from Monday (1) to Friday (5)).