js获取30天前日期

js获取30天前日期

var date1 = new Date();
var date2 = new Date(date1);

//-30为30天前,+30可以获得30天后的日期
date2.setDate(date1.getDate() - 30);

//30天前(月份判断是否小于10,小于10的前面+0)
var agoDay = `${ date2.getFullYear()}-${ date2.getMonth() + 1<10?`0${ date2.getMonth() + 1}`:date2.getMonth() + 1}-${ date2.getDate()}`; //当前日期 var nowDay = `${ date1.getFullYear()}-${ date1.getMonth() + 1<10?`0${ date1.getMonth() + 1}`:date1.getMonth() + 1}-${ date1.getDate()}`; console.log(`30天前:${ agoDay}`) console.log(`当前日期:${ nowDay}`)

《js获取30天前日期》

    原文作者:小李吖
    原文地址: https://blog.csdn.net/qq_42312077/article/details/106781123
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞