显示完整日期

时间:2015-10-27 00:37:57

标签: javascript

我试图显示完整日期。如何修改此脚本以显示完整日,而不仅仅是缩写。

e.g。 Monday October 26, 2015 Mon Oct 26 2015



var d = new Date();
document.getElementById("demo").innerHTML = d.toDateString();

<p id="demo"></p>
&#13;
&#13;
&#13;

2 个答案:

答案 0 :(得分:1)

This会帮助您:

var a = new Date();
var monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
var dayNames = ["Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"];

var date = dayNames[a.getDay()-1] + " " + monthNames[a.getMonth()] + " " + a.getDate() + " " + a.getFullYear();

document.getElementById("date").innerHTML = date;

使用类似的字符串作为星期几的名称

答案 1 :(得分:0)

你可以尝试这种方式。 date.getDay()将返回工作日的数字表示。根据日期或月份,您可以从数组中绘制不同的元素:

var date = new Date();
var day = date.getDay();
var month = date.getMonth();
var monthNames = ["January", "February", "March","April", "May","June", "July","August", "September", "October","November", "December"];
var dayNames = ['Monday','Tuesday','Wednesday','Thursday','Friday','Saturday','Sunday'];

alert(monthNames[month]);
alert(dayNames[day-1]);

演示代码:

http://jsfiddle.net/7e578hm8/2/