我一直试图使用脚本获得月份全名(如下所示),但无法得到它......似乎有些东西丢失了..
代码如下:
var d=new Date();
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
var timeString = d.getDate() + '-' + d.getFullYear()
+ ' ' + d.getHours() + ':' + d.getMinutes() + ':'
+ d.getSeconds();
document.getElementById("date").innerHTML = (monthNames[d.getMonth()]) + timeString;

<div>
<small><B><p id="date" name="date" class="date"></p></B></small>
</div>
&#13;
感谢任何帮助..
答案 0 :(得分:1)
var d=new Date();
Date.prototype.getMonthName = function() {
var monthNames = ["January", "February", "March", "April", "May", "June",
"July", "August", "September", "October", "November", "December"];
return monthNames[this.getMonth()];
}
var timeString = d.getDate() + '-' + d.getMonthName() + '-' + d.getFullYear()
+ ' ' + d.getHours() + ':' + d.getMinutes() + ':'
+ d.getSeconds();
document.getElementById("date").innerHTML =timeString;
您的代码没问题
答案 1 :(得分:0)
创建timeString变量时有一个简单的问题。而不是monthNames,你应该调用你的方法“getMonthName”而不是只询问你在getMonthName中创建的变量。
var timeString = d.getDate() + '-' + d.getMonthName() + '-' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
答案 2 :(得分:0)
这是一个小小的小提琴,可以做你想要的。
<div>
<p id="date" name="date" class="date">Date</p>
</div>
<script>
Date.prototype.getMonthName = function() {
var monthNames = ["January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"];
return monthNames[this.getMonth()];
};
var d = new Date();
var monthName = d.getMonthName();
var timeString = d.getDate() + '-' + monthName + '-' + d.getFullYear() + ' ' + d.getHours() + ':' + d.getMinutes() + ':' + d.getSeconds();
document.getElementById("date").innerHTML = timeString;
</script>
段落中的通知设置日期和时间不在原型声明之内。还要注意return
语句(不执行后的代码)!