日期未在JS日期应用中正确显示

时间:2019-05-06 16:09:00

标签: javascript date

我正在尝试制作一个简单的JS日历,该日历将指示当前日期,然后指示该日期之后的90个日期。我希望将其显示为例如:2019年5月6日,星期一-和2019年8月4日,星期日。您能告诉我为什么我的第二个日期显示不同吗?谢谢! (我已经在HTML文件中附加了代码)。

我能够将两个日期都设置为像05/06/2019和08/04/2019一样显示,但是当我尝试更改并使用数组显示为上述语法时,第二个日期无法正确显示

我创建了一个JS Fiddle,在这里显示代码和结果: https://jsfiddle.net/Catja_M/68q0s7fy/2/

var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new Array('January','February','March','April','May','June','July','August','September','October','November','December');
var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;}
today=days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear()));     
document.write(today);

var now = new Date();
var days = new Array('Sunday','Monday','Tuesday','Wednesday','Thursday','Friday','Saturday');
var months = new 

Array('January','February','March','April','May','June','July','August','September','October','November','十二月”)。

var date = ((now.getDate()<10) ? "0" : "")+ now.getDate();
function fourdigits(number) {
return (number < 1000) ? number + 1900 : number;}
var today=new Date()
new Date(days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear())));

today.setDate(today.getDate()+90)   
document.write(today);

希望两个日期都显示相同:2019年5月6日,星期一。

1 个答案:

答案 0 :(得分:0)

更改此内容:

var today=new Date()
new Date(days[now.getDay()] + ", " +
months[now.getMonth()] + " " +
date + ", " +
(fourdigits(now.getYear())));

today.setDate(today.getDate()+90)   
document.write(today);

收件人:

now.setDate(now.getDate() + 90)  //this returns a total millis number not a "date", it needs to be converted to day/month/year

var date = ((now.getDate()<10) ? "0" : "") + now.getDate();
function fourdigits(number) {
    return (number < 1000) ? number + 1900 : number;
}
var today = days[now.getDay()] + ", " + months[now.getMonth()] + " " + date + ", " +  fourdigits(now.getYear());
document.write(today);