欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  web前端

如何使用Javascript获取距今n天前的日期_javascript技巧

程序员文章站 2022-04-12 23:25:25
...
复制代码 代码如下:

function Test(day) {
var today = new Date();
var beforMilliseconds = today.getTime() - 1000 * 3600 * 24 * day;
var beforday = new Date();
beforday.setTime(beforMilliseconds);
var strYear = beforday.getFullYear();
var strDay = beforday.getDate();
var strMonth = beforday.getMonth() + 1;
if (strMonth strMonth = "0" + strMonth;
}
var strYesterday = strYear + "-" + strMonth + "-" + strDay;
alert(strYesterday);
}