js中获取本月第一天和最后一天的日期

获取本月第一天和最后一天的日期

话不多说直接上代码

export function getmonthDate() {
  var monthDate = {};
  var now = new Date(); //当前日期
  var nowMonth = now.getMonth(); //当前月
  var nowYear = now.getFullYear(); //当前年
  //本月的开始时间
  var monthStartDate = new Date(nowYear, nowMonth, 1);
  var yy = monthStartDate.getFullYear() + "-";
  var mm =
    (monthStartDate.getMonth() + 1 < 10
      ? "0" + (monthStartDate.getMonth() + 1)
      : monthStartDate.getMonth() + 1) + "-";
  var dd =
    monthStartDate.getDate() < 10
      ? "0" + monthStartDate.getDate()
      : monthStartDate.getDate();
  //本月的结束时间
  var monthEndDate = new Date(nowYear, nowMonth + 1, 0);
  var YY = monthEndDate.getFullYear() + "-";
  var MM =
    (monthEndDate.getMonth() + 1 < 10
      ? "0" + (monthEndDate.getMonth() + 1)
      : monthEndDate.getMonth() + 1) + "-";
  var DD =
    monthEndDate.getDate() < 10
      ? "0" + monthEndDate.getDate()
      : monthEndDate.getDate();
  monthDate.start_day = yy + mm + dd;
  monthDate.end_day = YY + MM + DD;
  return monthDate
  // this.params.start_day = yy + mm + dd; //本月的开始时间
  // this.params.end_day = YY + MM + DD; //本月的结束时间
}

这是封装在一个js里面 调用如下
先引入
import {getmonthDate,} from "文件路径";
然后在调用

console.log(getmonthDate())

猜你喜欢

转载自blog.csdn.net/ZHOU_CXY/article/details/105467062