使用Javascript显示当前日期

时间:2015-09-05 21:02:03

标签: javascript date

var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
document.write(n + getDate + getFullYear);

我在显示当前日期时遇到问题。任何帮助都会有所帮助。

1 个答案:

答案 0 :(得分:1)

看起来问题出在你对document.write的调用中。您需要在创建的日期对象上调用getDate和getFullYear(d)。见下文

var d = new Date();
var weekday = new Array(7);
weekday[0] = "Sunday";
weekday[1] = "Monday";
weekday[2] = "Tuesday";
weekday[3] = "Wednesday";
weekday[4] = "Thursday";
weekday[5] = "Friday";
weekday[6] = "Saturday";
var n = weekday[d.getDay()];
document.write(n + ', ' + d.getMonth() + '/' + d.getDate() + '/' + d.getFullYear());