我有这个脚本在我的网页上显示当前的日期和时间:
<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 :(得分: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 :(得分: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
位于1
到31
的范围内,这不是您dayendings
的指标阵列。
答案 2 :(得分: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);
答案 3 :(得分: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
答案 4 :(得分: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>
不要忘记页面头部的脚本标记: 前