Display day of the week with Javascript date

后端 未结 2 755
太阳男子
太阳男子 2020-12-20 22:47

I am using the code below to display a date 7 days in the future. However this javascript code formats the date by mm/dd/yyyy.

I would like to modify the javascript

相关标签:
2条回答
  • 2020-12-20 23:11

    Date has a getDay() method which returns a day number, starting with Sunday on 0 and continuing with Monday=1, Tuesday=2 and so on.

    You can use that to return just the week day. See How to get the day from a particular date using JavaScript and similar questions.

    document.write(["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"][myDate.getDay()]);
    
    0 讨论(0)
  • 2020-12-20 23:18

    try this , adding 1 week from current day output you get is :Friday,November 1

    <script >
    <!--
    
    var m_names = ["January", "February", "March", 
    "April", "May", "June", "July", "August", "September", 
    "October", "November", "December"];
    
    var d_names = ["Sunday","Monday", "Tuesday", "Wednesday", 
    "Thursday", "Friday", "Saturday"];
    
    var myDate = new Date();
    myDate.setDate(myDate.getDate()+7);
    var curr_date = myDate.getDate();
    var curr_month = myDate.getMonth();
    var curr_day  = myDate.getDay();
    document.write(d_names[curr_day] + "," + m_names[curr_month] + " " +curr_date);
    
    //-->
    </script>
    
    0 讨论(0)
提交回复
热议问题