碎片時間學(xué)編程「297]:以字符串表示形式計算從今天算起的幾天前的日期

使用Date構(gòu)造函數(shù)獲取當(dāng)前日期。
使用Math.abs()和 Date.prototype.getDate()方法相應(yīng)地更新日期并使用 Date.prototype.setDate() 設(shè)置為結(jié)果。
使用Date.prototype.toISOString()以格式返回字符串yyyy-mm-dd。
JavaScript
const daysAgo = n => { ?let d = new Date(); ?d.setDate(d.getDate() - Math.abs(n)); ?return d.toISOString().split('T')[0];};
示例:
daysAgo(20); // 2020-09-16 (if current date is 2020-10-06)
更多內(nèi)容請訪問我的網(wǎng)站:https://www.icoderoad.com
標(biāo)簽: