-1

我一直在尝试显示以下“在2022年14

在 HTML 页面上使用 javascript

const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDay()
var dayString;
if (dayn == 1) {
  dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
  dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
  dayString = "</b><sup>th</sup>"
} else {
  dayString = "</b><sup>nd</sup>"
}
var dateStr = "<b>" + dayn + dayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(dateStr);

但由于某种原因,它今天显示为第 2 次而不是第 4

我想显示的地方,即。2022 年 3 月4(今天 +89 天)它没有这样做......

所以目的是在第一行显示:

202214

在下一行

“不迟于2022 年 33日”

4

2 回答 2

2

你必须使用d.getDate()而不是d.getDay()

const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDate();
var endDate = d.setDate(dayn + 89);
var daym = new Date(endDate).getDate();

var dayString;
if (dayn == 1) {
  dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
  dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
  dayString = "</b><sup>th</sup>"
} else {
  dayString = "</b><sup>nd</sup>"
}

var endDayString;
if (daym == 1) {
  endDayString = "</b><sup>st</sup>"
} else if (daym == 3) {
  endDayString = "</b><sup>rd</sup>"
} else if (daym >= 4) {
  endDayString = "</b><sup>th</sup>"
} else {
  endDayString = "</b><sup>nd</sup>"
}
 
var endDateStr = "<b>" + daym + endDayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(endDateStr);
于 2022-01-04T06:46:22.683 回答
0
const monthNames = ["January", "February", "March", "April", "May", "June",
  "July", "August", "September", "October", "November", "December"
];
const d = new Date();
var dayn = d.getDate();
var dayString;
if (dayn == 1) {
  dayString = "</b><sup>st</sup>"
} else if (dayn == 3) {
  dayString = "</b><sup>rd</sup>"
} else if (dayn >= 4) {
  dayString = "</b><sup>th</sup>"
} else {
  dayString = "</b><sup>nd</sup>"
}
var dateStr = "<b>" + dayn + dayString + "</b> day of<b> " + monthNames[d.getMonth()] + "</b> in the year <b>" + d.getFullYear(); + "</b>"
document.write(dateStr);
于 2022-01-04T06:54:06.230 回答