Determine if a date is a Saturday or a Sunday using JavaScript

后端 未结 6 1174
忘了有多久
忘了有多久 2020-12-08 01:54

Is it possible to determine if a date is a Saturday or Sunday using JavaScript?

Do you have the code for this?

6条回答
  •  谎友^
    谎友^ (楼主)
    2020-12-08 02:14

    Yes, it is possible, we can write a JavaScript code for that using JavaScript Date object.

    Please use following JavaScript code.

    var d = new Date()

    document.write(d.getDay())

    We can write a function to return the weekend in flag like below, You can more customize the function to pass date. Or different return values for every day.

        isItWeekEnd = function() {
        var d = new Date();
        console.log(d.getDay());
        var dateValue = d.getDay(); 
        // dateValue : 0 = Sunday
        // dateValue : 6 = Saturday
        if(dateValue == 0 || dateValue == 6)
            return true;
        else 
            return false;  
    }
    

提交回复
热议问题