我正在试着做一个基本的周日历。数字运行得很好,但我不知道如何准确地实现这一点,以字符串形式获取天数。例如,我需要这样的格式:今天是:星期一明天将是:星期二
我尝试了一些代码,但我所能做的就是这样写一天:今天是:星期一。
那么有没有把“星期一”改成“星期一”的选择呢?
我也需要这样做,才能得到明天和昨天的价值。我的意思是:昨天:今天:星期一:星期二
但当我试图做到这一点时,出现了一个错误。你知道如何让它变得可行吗?
顺便说一句,我的尝试如下:
var options = {
weekday: 'long'
};
var today = new Date();
var option = {
weekday: 'long'
};
var tomorrow = new Date();
var todaya1 = tomorrow.getDate() + 1;
document.getElementById("today").innerHTML = today.toLocaleDateString("en-US", options);
document.getElementById("tomorrow").innerHTML = todaya1.toLocaleDateString("en-US", option);#today {
color: red;
}<span id="today"></span>
<span id="tomorrow"></span>
发布于 2021-01-04 15:15:36
您有控制台错误,因为tomorrow.getDate() + 1;不是date对象
您需要创建两个date对象
此外,如果一组选项相同,则只需要一组选项
const options = { weekday: 'short' };
var today = new Date();
var tomorrow = new Date()
tomorrow.setDate(tomorrow.getDate() + 1);
document.getElementById("today").innerHTML = today.toLocaleDateString("en-US", options);
document.getElementById("tomorrow").innerHTML = tomorrow.toLocaleDateString("en-US", options);#today {
color: red;
}<span id="today"></span>
<span id="tomorrow"></span>
发布于 2021-01-04 15:23:31
const days = ['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'];
const todayDate = new Date();
const todayDay = days[todayDate.getDay()];
const tomorrowDate = new Date(todayDate);
tomorrowDate.setDate(tomorrowDate.getDate() + 1);
const tomorrowDay = days[tomorrowDate.getDay()];
const todayEl = document.getElementById('today');
const tomorrowEl = document.getElementById('tomorrow');
todayEl.textContent = todayDay;
tomorrowEl.textContent = tomorrowDay;<span id="today"></span>
<span id="tomorrow"></span>
https://stackoverflow.com/questions/65558908
复制相似问题