首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >JavaScript日期格式:格式化日期e.x (星期一)

JavaScript日期格式:格式化日期e.x (星期一)
EN

Stack Overflow用户
提问于 2021-01-04 15:11:54
回答 2查看 32关注 0票数 0

我正在试着做一个基本的周日历。数字运行得很好,但我不知道如何准确地实现这一点,以字符串形式获取天数。例如,我需要这样的格式:今天是:星期一明天将是:星期二

我尝试了一些代码,但我所能做的就是这样写一天:今天是:星期一。

那么有没有把“星期一”改成“星期一”的选择呢?

我也需要这样做,才能得到明天和昨天的价值。我的意思是:昨天:今天:星期一:星期二

但当我试图做到这一点时,出现了一个错误。你知道如何让它变得可行吗?

顺便说一句,我的尝试如下:

代码语言:javascript
复制
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);
代码语言:javascript
复制
#today {
  color: red;
}
代码语言:javascript
复制
<span id="today"></span>
<span id="tomorrow"></span>

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2021-01-04 15:15:36

您有控制台错误,因为tomorrow.getDate() + 1;不是date对象

您需要创建两个date对象

此外,如果一组选项相同,则只需要一组选项

代码语言:javascript
复制
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);
代码语言:javascript
复制
#today {
  color: red;
}
代码语言:javascript
复制
<span id="today"></span>
<span id="tomorrow"></span>

票数 2
EN

Stack Overflow用户

发布于 2021-01-04 15:23:31

代码语言:javascript
复制
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;
代码语言:javascript
复制
<span id="today"></span>
<span id="tomorrow"></span>

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65558908

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档