JavaScript 获取本月第一天和本月最后一天。上个月的第一天和最后一天

 

new Date() 获取当前的函数。

//本月的第一天
var date = new Date();
  date.setDate(1);

 

//上个月的第一天
var year = date.getFullYear(),
  month = date.getMonth();

  if( month == 0){
    month=12;
    year=year-1;
  }

  if( month < 10) {
    month = "0" + month;
  }

  var firstDay = year + "-" + month + "-" + "01";

  //上个月的最后一天
   var lastDate = new Date();
     lastDate.setDate(0);

posted @ 2016-04-06 19:26  Alex_zeng  阅读(1162)  评论(0编辑  收藏  举报