现在,我的代码是这样的:

var day = getDay(); //A function that gets this current day of the week as integer
var dayname = "";
if (day==0) dayname = "Su";
if (day==1) dayname = "Mo";
...
if(day==6) dayname = "Sa";


它可以工作,但我觉得它可能会更短一些,如下所示:

if (day == (0,1,2,3,4,5,6)) dayname = ("Su","Mo",...,"Sa");


有没有办法做到这一点?

最佳答案

var days = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
var dayname = days[getDay()];

09-17 02:57