[英]Show current date and time in javascript
I have this script to display the current date and time in my web page: 我有此脚本可在网页中显示当前日期和时间:
<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>
But the result is like this: 但是结果是这样的:
Friday, April NaN, 2012 6:37:36 PM
How can I make April NaN, 2012
right? 我怎样才能使
April NaN, 2012
正确? It must be the date today which is May 25,2012
今天必须是
May 25,2012
At the very least you should change 至少你应该改变
da+=endings[dayendings[da]];
to 至
da+=endings[dayendings[da - 1]];
because right now it has an off-by-one bug ( da
is 1 to 31, but the indexes on dayendings
are 0 to 30). 因为现在它有一个错误的错误(
da
为1到31,但是dayendings
的索引为0到30)。
This should immediately fix your problem if "today" is the 31st of the month. 如果“今天”是每月的31号,这应该立即解决您的问题。
Fixes are also needed for the values in dayendings
, as 4
is not meaningful there. 还需要修正
dayendings
的值,因为那里4
没有意义。
On a side note, you really should ditch this code for something better . 附带说明一下,您确实应该放弃此代码以获取更好的东西 。 Wouldn't you prefer it like this?
您不喜欢这样吗?
function startTime() {
document.getElementById("time").innerHTML =
moment().format("dddd, MMMM Do, YYYY h:mm:ss A");
}
Your 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 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];
includes some 4
values. 包括一些
4
值。 Yet, var endings=["st","nd","rd","th"];
然而,
var endings=["st","nd","rd","th"];
has only a length of 4, so that it will return undefined when you access endings[4]
. 仅具有4的长度,因此当您访问
endings[4]
时它将返回未定义。
Also, you may get an off-by-one-error because da
is in the range 1
to 31
, which are not the indizes of your dayendings
array. 另外,由于
da
在1
到31
的范围内,因此您可能会遇到一个错误,这不是您的dayendings
数组的dayendings
。
It appears that your logic for the counter is not correct. 您的计数器逻辑似乎不正确。 Try the following code:
尝试以下代码:
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);
I've a relatively simple DateTime object in this jsfiddle . 我在这个jsfiddle中有一个相对简单的DateTime对象。 May be useful?
可能有用吗? In your case, using this object, this command would show the date in the format you want (apart from the PM/AM time formatting)
在您的情况下,使用此对象,此命令将以您想要的格式显示日期(除PM / AM时间格式外)
XDate({date:new Date,lang:'EN'}).format('WD, MM dd, yyyy, hh:mm:se0');
//=> Friday, May 25 2012, 13:05:03
first generate this function and save as .js file 首先生成此函数并另存为.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;
}
then use this code on your html page to display the current time and date 然后在html页面上使用此代码显示当前时间和日期
<script type="text/javascript"> window.onload = date_time('date_time'); </script>
dont forget the script tag on the head part of your page: ex 不要忘记页面顶部的脚本标签:ex
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.