Javascript 日期对象关闭一个月
我知道 javascript 从 0 开始计算月份。但是,我不知道在将来获取日期时如何考虑这一点。
例如,当尝试获取明天的日期时,以下代码将返回除月份之外的所有正确内容(打印为下个月):
const weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var today = new Date();
today.setUTCDate(today.getUTCDate()+1);
let newDate = weekday[today.getDay()] +' '+today.getDate()+' '+month[today.getDay()]+', '+ today.getFullYear();
console.log(newDate)
I understand that javascript counts the months starting from 0. However, I don't know how to account for this when getting a date in the future.
For example, when attempting to get the date for tomorrow, the following code returns everything correct except for the month (prints as next month):
const weekday = ["Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday"];
const month = ["Jan","Feb","Mar","Apr","May","Jun","Jul","Aug","Sep","Oct","Nov","Dec"];
var today = new Date();
today.setUTCDate(today.getUTCDate()+1);
let newDate = weekday[today.getDay()] +' '+today.getDate()+' '+month[today.getDay()]+', '+ today.getFullYear();
console.log(newDate)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需将
today.getDay()
替换为today.getMonth()
Just replace
today.getDay()
totoday.getMonth()