根据指定时间获取当前是本月的第几周

function getWeekOfMonth(date) {
  var firstDayOfMonth = new Date(date.getFullYear(), date.getMonth(), 1);
  var dayOfWeek = firstDayOfMonth.getDay() || 7; // 转换星期天为7
  var startOfWeek = firstDayOfMonth.setDate(1 - dayOfWeek + 1);
  var diff = date - startOfWeek;
  var week = Math.ceil(diff / (1000 * 60 * 60 * 24 * 7)); // 毫秒转换为周
  return week;
}
getWeekOfMonth(new Date("2023-09-04"))

你可能感兴趣的:(JavaScript,javascript,开发语言,ecmascript)