我有這樣的腳本,以顯示我的網頁的當前日期和時間:顯示當前日期和時間在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
你想要'25th'還是'25'? – Amberlamps