常用js 时间函数

//本月

var d = new Date();
               d.setDate(1);
               var first_day = d.getFullYear() + '-' + (d.getMonth() +1) + '-' + d.getDate();
               d.setMonth(d.getMonth() +1);
               d.setDate(0);
               var last_day = d.getFullYear() + '-' + (d.getMonth() +1) + '-' + d.getDate();

console.log(first_day);
console.log(last_day);

 

//本周

 

var d = new Date();
               var n = d.getDay();
               var f = d.getDate() - n + 1;
               var l = d.getDate() + 7 - n;
               d.setDate(f);
               var first_day = d.getFullYear() + '-' + (d.getMonth() +1) + '-' + d.getDate();
               d.setDate(l);
               var last_day = d.getFullYear() + '-' + (d.getMonth() +1) + '-' + d.getDate();

 

console.log(first_day);
console.log(last_day);
 

你可能感兴趣的:(javascript)