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

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

    The Date class offers the getDay() Method that retrieves the day of the week component of the date as a number from 0 to 6 (0=Sunday, 1=Monday, etc)

    var date = new Date();
    switch(date.getDay()){
        case 0: alert("sunday!"); break;
        case 6: alert("saturday!"); break;
        default: alert("any other week day");
    }
    

提交回复
热议问题