可以使用JavaScript的Date对象来获取当前月份的第一天和最后一天。

首先,我们可以使用new Date()来获取当前日期的Date对象。

然后,我们可以使用setDate(1)将日期设置为当前月份的第一天。

接着,我们可以使用setMonth(getMonth() + 1)将月份设置为下一个月,然后使用setDate(0)将日期设置为当前月份的最后一天。

最后,我们可以使用toISOString().slice(0, 10)方法将Date对象转换成"YYYY-MM-DD"的格式。

以下是一个示例代码:

function getCurrentMonthFirstAndLastDay() {
  var today = new Date();
  var firstDay = new Date(today.getFullYear(), today.getMonth(), 1);
  var lastDay = new Date(today.getFullYear(), today.getMonth() + 1, 0);
  
  var firstDayFormatted = firstDay.toISOString().slice(0, 10);
  var lastDayFormatted = lastDay.toISOString().slice(0, 10);
  
  return {
    firstDay: firstDayFormatted,
    lastDay: lastDayFormatted
  };
}

var result = getCurrentMonthFirstAndLastDay();
console.log(result.firstDay);
console.log(result.lastDay);

运行以上代码,你将会得到当前月份的第一天和最后一天的日期,例如"2023-07-01"和"2023-07-31"

js获取当前月份的第一天和最后一天例如2023-07-012023-07-31

原文地址: https://www.cveoy.top/t/topic/ibNJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录