且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

使用 moment.js 获取本月的第一个工作日

更新时间:2022-12-08 15:23:00

如果第一天是星期天或星期六 (first.day() % 6 === 0) 然后返回下星期一 (first.day(1)):

If the first day is sunday or saturday (first.day() % 6 === 0) then return next monday (first.day(1)):

function dateStart() {
  var first = moment().startOf('month');
  return first.day() % 6 === 0 ? first.add(1, 'day').day(1) : first;
}

正如评论中提到的 first.day(1) 可以返回上个月的星期一.如果一个月的第一天是星期六,就会发生这种情况.为确保您从当月的一周中获得星期一,只需将周末日期加 1.

As mentioned in comments first.day(1) can return monday in previous month. This can happen if the first day of the month is saturday. To be sure you get monday from the week in current month just add 1 to the weekend date.