2015-05-17 64 views
-2

我是一個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; 
} 

它工作正常,但我需要知道這一點最好的代碼是否我能寫,或者如果我能有所改進。

+2

什麼是'statements_1'? –

+2

'switch'語句應該是什麼好處?您可以通過日期編號直接訪問正確的數組元素,根本不需要switch語句。 – CBroe

+0

[如何獲取星期幾和一年中的月份]可能的重複(http://stackoverflow.com/questions/4822852/how-to-get-the-day-of-week-and-the - 年的月) –

回答

-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>'); 
+0

@S Mac是謝謝你,我在想這個.. :) –

+0

'document.write'?我希望DOM API更容易。我覺得我看到它時需要淋浴。 :/ – Sukima