星期几周

时间:2015-05-17 02:27:00

标签: javascript

我是一个javascript新手。我正在学习,我正在努力获得星期几,并将其写入文档。我正在使用阵列和开关盒。

检查我的代码:

var currentDate=new Date();
var currentDay=currentDate.getDay();
var daysOfWeek=["Sunday","Monday","Tuesday","Wed","Thu","Fri","Sat"];

switch(currentDay){
    case 0:
    document.write('Today is  <b>' +daysOfWeek[0]);

    break;
    case 1:

    break;
    case 2:

    break;
    case 3:

    break;
    case 4:

    break;
    case 5:

    break;

    case 6:

    break;
    default:
    break;
}

它工作正常,但我需要知道这是否是我能写的最好的代码,或者我是否可以改进它。

1 个答案:

答案 0 :(得分:-1)

这样做的一个更好的方法(保持一般方法)将是:

var currentDate=new Date();
var currentDay=currentDate.getDay();
var daysOfWeek=["Sunday","Monday","Tuesday","Wed","Thu","Fri","Sat"]; 
document.write('Today is  <b>' +daysOfWeek[currentDay] + '</b>');