2012-05-25 87 views
0

我有這樣的腳本,以顯示我的網頁的當前日期和時間:顯示當前日期和時間在javascript

<script type = "text/javascript"> 

function startTime() { 
var now = new Date(); 
var h=now.getHours(); 
var min=now.getMinutes(); 
var s=now.getSeconds(); 
var ampm=(now.getHours()>11)?"PM":"AM"; 
var d=now.getDay(); 
var y=now.getFullYear(); 
var mon=now.getMonth(); 
var da=now.getDate(); 
var endings=["st","nd","rd","th"]; 
var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1]; 
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"]; 
var months=["January","February","March","April", "May", "June","July","August","September", "October", "Novemeber","Decemeber"]; 
if (h>12) {h-=12} 
if (h==0) {h=12}; 
if (min<10) {min="0"+min} 
if (s<10) {s="0"+s} 
da+=endings[dayendings[da]]; 
if (da<10) {da="0"+da}; 
d=days[d]; 
mon=months[mon-1]; 
document.getElementById("time").innerHTML=d+", "+mon+" "+da+", "+y+" "+h+":"+min+":"+s+" "+ampm; 
var tim = setTimeout("startTime()",1000); 
} 

</script> 

但結果是這樣的:

Friday, April NaN, 2012 6:37:36 PM 

我怎樣才能使April NaN, 2012對不對?它必須是日也就是今天May 25,2012

+0

你想要'25th'還是'25'? – Amberlamps

回答

4

在最起碼你應該改變

da+=endings[dayendings[da]]; 

da+=endings[dayendings[da - 1]]; 

,因爲現在它有一個關閉的情況的一個錯誤(da是1至31,但dayendings上的索引爲0至30)。

如果「今天」是本月的31日,這應該立即解決您的問題。

dayendings中的值也需要修復,因爲4在那裏沒有意義。

在附註中,您應該確認這個代碼爲something better。你不喜歡這樣嗎?

function startTime() { 
    document.getElementById("time").innerHTML = 
     moment().format("dddd, MMMM Do, YYYY h:mm:ss A"); 
} 
+1

+1時刻。 –

+0

它沒有工作:( –

+0

@AlaiAngelo:[Works for me](http://jsfiddle.net/defacer/P6aCV/)。 – Jon

2

您的var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1];包含一些4值。然而,var endings=["st","nd","rd","th"];只有4的長度,所以當您訪問endings[4]時它將返回undefined。

此外,您可能因爲da範圍爲131,這是不是你的dayendings陣列的indizes得到的off-by-一個錯誤。

0

看來您的計數器邏輯不正確。 試試下面的代碼:

function makeArray() { 
for (i = 0; i<makeArray.arguments.length; i++) 
this[i + 1] = makeArray.arguments[i]; 
} 
var now = new Date(); 
var h=now.getHours(); 
var min=now.getMinutes(); 
var s=now.getSeconds(); 
var ampm=(now.getHours()>11)?"PM":"AM"; 
var months = new makeArray('January','February','March','April','May', 
'June','July','August','September','October','November','December'); 
var date = new Date(); 
var day = date.getDate(); 
var month = date.getMonth() + 1; 
var yy = date.getYear(); 
var year = (yy < 1000) ? yy + 1900 : yy; 
var endings=["st","nd","rd","th"]; 
var dayendings=[0,1,2,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,0,2,3,4,4,4,4,4,4,1]; 
var days=["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"]; 

document.write(days[date.getDay()] + " " + months[month]+ " " + day + ", " + year + " " + h + ":" + min + ":" + s + " " + ampm); 
+0

這個工程,但我需要的時間刷新在每一秒 –

+0

在這種情況下,只需要調用自己的函數。立即在document.write之後var tim = settimout(...)...此鏈接可以給你舉個例子。希望這可以幫助。 http://www.w3schools.com/js/js_timing.asp – SoftwareCarpenter

+1

作爲一個便箋,下面的答案更清晰,所以一旦你在它需要更新的地方工作,你可能需要重新考慮腳本。 – SoftwareCarpenter

0

this jsfiddle已經相對簡單的DateTime對象。可能有用嗎?在你的情況,使用對象,這個命令將顯示你想要的格式的日期(除PM/AM時間格式)

XDate({date:new Date,lang:'EN'}).format('WD, MM dd, yyyy, hh:mm:se0'); 
//=> Friday, May 25 2012, 13:05:03 
0

第一生成此功能並保存爲.js文件

function date_time(id) { 
    date = new Date; 
    year = date.getFullYear(); 
    month = date.getMonth(); 
    months = new Array('January', 'February', 'March', 'April', 'May', 'June', 'Jully', 'August', 'September', 'October', 'November', 'December'); 
    d = date.getDate(); 
    day = date.getDay(); 
    days = new Array('Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'); 
    h = date.getHours(); 
    if (h < 10) { 
     h = "0" + h; 
    } 
    m = date.getMinutes(); 
    if (m < 10) { 
     m = "0" + m; 
    } 
    s = date.getSeconds(); 
    if (s < 10) { 
     s = "0" + s; 
    } 
    result = '' + days[day] + ' ' + months[month] + ' ' + d + ' ' + year + ' ' + h + ':' + m + ':' + s; 
    document.getElementById(id).innerHTML = result; 
    setTimeout('date_time("' + id + '");', '1000'); 
    return true; 
} 

然後用你的HTML頁面上此代碼來顯示當前的時間和日期

<script type="text/javascript"> window.onload = date_time('date_time'); </script> 

不要忘記你的頁面頭部的腳本標籤: 前