js获取30、X天前得时间

//获取当前日期

var date = new Date();

var year = date.getFullYear();

var month = date.getMonth()+1;

var day = date.getDate();
var nowDate = year + "-" + (month < 10 ? "0" + month : month) + "-" + (day < 10 ? "0" + day : day);
//获取30天前得日期
var lastDate = new Date(date - 1000 * 60 * 60 * 24 * 30);//最后30天可以更改,意义:是获取多少天前的时间
var lastY = lastDate.getFullYear();
var lastM = lastDate.getMonth()+1;
var lastD = lastDate.getDate();
var LDate = lastY + "-" + (lastM < 10 ? "0" + lastM : lastM) + "-"(lastD < 10 ? "0" + lastD : lastD);//得到30天前的时间

 希望对您有所帮助

posted @ 2018-08-01 16:27  记忆中的梦境  阅读(7485)  评论(0编辑  收藏  举报