How to get the day from a particular date using JavaScript

前端 未结 3 644
情歌与酒
情歌与酒 2020-12-07 01:29

I am new to JavaScript. My requirement is that T want to pop up a message on particular days (like Sunday, Monday...) all through when a date is selected.

I tried

相关标签:
3条回答
  • 2020-12-07 02:07
    var date = new Date();
    var day = date.getDay();
    

    day now holds a number from zero to six; zero is Sunday, one is Monday, and so on.

    So all that remains is to translate that number into the English (or whatever other language) string for the day name:

    var name = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][day];
    
    0 讨论(0)
  • 2020-12-07 02:14

    This page seems to provide you with what you need.

    What we are going to do is to add getMonthName() and getDayName() methods to all our dates so that we can get the month name or day name by calling these new methods directly instead of having to call getMonth() or getDay() and then do an array lookup of the corresponding name.

    You can then do:

    var today = new Date;
    alert(today.getDayName());
    
    0 讨论(0)
  • 2020-12-07 02:27
    var days= ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    var today = new Date();
    document.write(days[today.getDay()]);
    
    0 讨论(0)
提交回复
热议问题