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

后端 未结 6 1171
忘了有多久
忘了有多久 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:12

    Sure it is! The Date class has a function called getDay() which returns a integer between 0 and 6 (0 being Sunday, 6 being Saturday). So, in order to see if today is during the weekend:

    var today = new Date();
    if(today.getDay() == 6 || today.getDay() == 0) alert('Weekend!');
    

    In order to see if an arbitrary date is a weekend day, you can use the following:

    var myDate = new Date();
    myDate.setFullYear(2009);
    myDate.setMonth(7);
    myDate.setDate(25);
    
    if(myDate.getDay() == 6 || myDate.getDay() == 0) alert('Weekend!');
    

提交回复
热议问题