微信小程序 js 获取当前月份的天数

// 计算这个月有多少天
  getCurrentMonthDayNum:function () {
    let today = new Date();
    let dayAllThisMonth = 31;
    if (today.getMonth() + 1 != 12) {
      let currentMonthStartDate = new Date(today.getFullYear() + "/" + (today.getMonth() + 1) + "/01"); // 本月1号的日期
      let nextMonthStartDate = new Date(today.getFullYear() + "/" + (today.getMonth() + 2) + "/01"); // 下个月1号的日期
      dayAllThisMonth = (nextMonthStartDate - currentMonthStartDate) / (24 * 3600 * 1000);
    }

    return dayAllThisMonth;
  },

逻辑很简单,如果是12月份直接返回31天,否则用下个月1号的时间减去本月1号的时间,然后计算时间间隔。

代码摘于自己的小程序“ 时间百分比”:

微信小程序 js 获取当前月份的天数