js 获取今天以及前一周/前20天时间

var end = new Date();
var start = new Date();
console.log('==========今天=============')
console.log(start);
console.log(end);
console.log('==========今天=============')
console.log(start.getFullYear());
console.log(start.getMonth());
console.log(start.getDate());
console.log('==========20天前=============')
start.setDate(start.getDate() - 20);   //day和month会自动计算到上一个月的
console.log(start.getFullYear());
console.log(start.getMonth());
console.log(start.getDate());


var startYear = start.getFullYear();
var startMonth = start.getMonth() + 1 >= 10 ? start.getMonth() + 1 : "0" + (start.getMonth() + 1);
var startDate = start.getDate() >= 10 ? start.getDate() : "0" + start.getDate();

var endYear = end.getFullYear();
var endtMonth = end.getMonth() + 1 >= 10 ? end.getMonth() + 1 : "0" + (end.getMonth() + 1);
var endDate = end.getDate() >= 10 ? end.getDate() : "0" + end.getDate();
startDate1 = startYear + "-" + startMonth + "-" + startDate;
endDate1 = endYear + "-" + endtMonth + "-" + endDate;
console.log(startDate1);
console.log(endDate1)

  console输出:

 

posted @ 2018-07-16 13:51  Andrew_F  阅读(708)  评论(0编辑  收藏  举报