九州編程
2021-10-14 17:13:26
我正在使用此函數(shù)來mm/dd/yy使用 javascript獲取明天的內(nèi)容:(dt.getMonth() + 1) + "/" + (dt.getDate() +1 ) + "/" + dt.getFullYear()今天的日期是 9/12/19,上面的代碼返回 9/13/19。但是,我不確定此代碼在9/30/2019. 我不確定如何測試這種情況。這是返回10/1/2019還是返回9/31/2019不正確。
1 回答

qq_遁去的一_1
TA貢獻1725條經(jīng)驗 獲得超8個贊
最簡單的方法是獲取一個日期對象并添加 1 天。它將正確滾動到下個月:
const date = new Date();
date.setDate(date.getDate() + 1);
console.log(`${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`);
測試月底場景:
const date = new Date(2019, 8, 30); // September 30
date.setDate(date.getDate() + 1);
console.log(`${date.getMonth() + 1}/${date.getDate()}/${date.getFullYear()}`);
您需要的格式對應于英語語言環(huán)境的格式,因此您也可以使用它而不是手動格式化:
console.log(new Date().toLocaleDateString('en', {day: 'numeric', month: 'numeric', year: 'numeric'}));
添加回答
舉報
0/150
提交
取消