Javascript 日期对象相差一个月
Javascript Date Object Off by One Month
我知道 javascript 从 0 开始计算月份。但是,我不知道在将来获取日期时如何考虑这一点。
例如,当尝试获取明天的日期时,以下代码 returns 除了月份之外的所有内容都是正确的(打印为下个月):
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)
只需将today.getDay()
替换为today.getMonth()
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.getMonth()]+', '+ today.getFullYear();
console.log(newDate)
我知道 javascript 从 0 开始计算月份。但是,我不知道在将来获取日期时如何考虑这一点。
例如,当尝试获取明天的日期时,以下代码 returns 除了月份之外的所有内容都是正确的(打印为下个月):
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)
只需将today.getDay()
替换为today.getMonth()
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.getMonth()]+', '+ today.getFullYear();
console.log(newDate)