Javascript日期问题

时间:2018-02-19 06:20:14

标签: javascript

我知道我的尝试很可怕,但这就是我所得到的。我试图以这种方式显示当前日期:2018年2月19日星期一。任何人都可以将这个混乱修复为可用的Javascript代码吗?

var dayname = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November", "December");

var d=new Date();
var today = dayname[d.getDay()] + ", " + d.getDate() + " " + monthname[d.getMonth()] + " " + d.getFullYear();
document.getElementById('currentdate").innerHTML = today;

2 个答案:

答案 0 :(得分:1)

这里有语法错误'currentdate"使用全部""'',否则您的代码工作正常......



var dayname = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November", "December");

var d=new Date();
var today = dayname[d.getDay()] + ", " + d.getDate() + " " + monthname[d.getMonth()] + " " + d.getFullYear();
document.getElementById('currentdate').innerHTML = today;
// Put a ' instead of "                 ^ here

<div id="currentdate"></div>
&#13;
&#13;
&#13;

我建议你使用的方法是仍然使用moment.js

&#13;
&#13;
var date = moment().format("dddd, Do MMMM YYYY");
console.log(date);
&#13;
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js"></script>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

&#13;
&#13;
var dayname = new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");

var monthname = new Array("January","February","March","April","May","June","July","August","September","October","November", "December");

var d=new Date();
var today = dayname[d.getDay()] + ", " + d.getDate() + " " + monthname[d.getMonth()] + " " + d.getFullYear();
document.getElementById("currentdate").innerHTML = today;
&#13;
<span id="currentdate"></span>
&#13;
&#13;
&#13;

document.getElementById('currentdate")这就是问题所在。您使用了一个'"