这段代码如何知道今天是星期二?
var today = new Date ();
var day = today.getDay();
var daylist = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
console.log("Today is : " + daylist[day] + ".");
答案 0 :(得分:0)
您应该阅读.getDay()
的手册:
getDay()
返回的值是与星期几对应的整数:星期日为0
,星期一为1
,星期二为2
,依此类推
以上解释了所有。 Date
对象从系统获取当前日期(和时间),这是基本的事情,然后包含有关日,月,年,周,周数等的所有详细信息。此外,调试您的代码,它具有以下内容:
var today = new Date();
// Returns `Tue Jan 05 2016 16:30:25 GMT+0000 (GMT Standard Time)`.
var day = today.getDay();
// Returns `2`, meaning it is Tuesday.
var daylist = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
// Okay, your array.
console.log("Today is : " + daylist[day] + ".");
// This is translated to `daylist[2]`, and Daylist's `2` has Tuesday.