skip to Main Content

why Javascript take the 3rd arrayList in this subject :

var today = new Date();
var day=today.getDate();
var daylist = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
console.log("Today is : " + daylist[day] + ".");

/**Today is : Tuesday.*//

I try to take the reason why the 3rd Arraylist

2

Answers


  1. The getDate() function returns today’s date like 2. so it always returns Tuesday.
    To get today’s day you should use the getDay() function.

    var today = new Date();
    var day=today.getDay();
    var daylist = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
    console.log("Today is : " + daylist[day] + ".");
    
    Login or Signup to reply.
  2. In JavaScript, getDay() returns a day-of-week number (0 for Sunday, 1 for Monday, etc.), and your daylist array is indexed using this value, making it access the corresponding day name.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search