最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Get previous business day with moment.js - Stack Overflow

programmeradmin3浏览0评论

What's the best way to get the previous business day's date with moment.js? A business day is Monday through Friday.

Some expectations:

  • If today is Satuday, Sunday or Monday, return last Friday's date
  • If today is Tuesday, return last Monday's date (yesterday)

What's the best way to get the previous business day's date with moment.js? A business day is Monday through Friday.

Some expectations:

  • If today is Satuday, Sunday or Monday, return last Friday's date
  • If today is Tuesday, return last Monday's date (yesterday)
Share Improve this question asked Apr 13, 2016 at 18:04 AsclepiusAsclepius 63.3k19 gold badges186 silver badges156 bronze badges
Add a comment  | 

5 Answers 5

Reset to default 8
function getPreviousWorkday(){
  let workday = moment();
  let day = workday.day();
  let diff = 1;  // returns yesterday
  if (day == 0 || day == 1){  // is Sunday or Monday
    diff = day + 2;  // returns Friday
  }
  return workday.subtract(diff, 'days');
}

Updated Approach (without looping)

You could actually take advantage of the day() function that would allow you to set the current day of the week in moment.js to find the previous Friday based on certain days :

function getPreviousWorkday(){
  // Based on the current day, handle accordingly
  switch(moment().day())
  {
      // If it is Monday (1),Saturday(6), or Sunday (0), Get the previous Friday (5)
      // and ensure we are on the previous week
      case 0:
      case 1:
      case 6:
        return moment().subtract(6,'days').day(5);
      // If it any other weekend, just return the previous day
      default:
        return moment().day(today - 1);
  }
}

which can be seen here and demonstrated below :

Looping Approach

You could simply subtract days from your current moment instance via the subtract() function from the current day until you reached a non-weekend day:

function getPreviousWorkday(){
  // Get today
  var today = new moment().subtract(-1,'days');;
  // If today isn't a weekend, continue iterating back until you hit a non-weekend
  while([0,6].indexOf(today.day()) !== -1){
    today = today.subtract(1, 'days');
  }
  // Return the non-weekend day
  return today;
}

You can see an example of this in action here and demonstrated below :

There is a npm module for that!

https://github.com/kalmecak/moment-business-days

From documentation:

prevBusinessDay() : Will retrieve the previous business date as moment date object:

//Previous busines day of Monday 02-02-2015
moment('02-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d // Fri Jan 30 2015 00:00:00 GMT-0600 (CST)

//Previous busines day of Tuesday 03-02-2015
moment('03-02-2015', 'DD-MM-YYYY').prevBusinessDay()._d //Mon Feb 02 2015 00:00:00 GMT-0600 (CST)

P.S: Important node. Why to use dependency? If all you need is prev business day - no reason to use 3-d party lib. But for real application all that stuff will be useful for more complex operations like importing dates, formatting and calculations based on calendars, holidays, etc.

function getPreviousWorkday() {
  return [1, 2, 3, 4, 5].indexOf(moment().subtract(1, 'day').day()) > -1 ? 
    moment().subtract(1, 'day') : moment(moment().day(-2));
}

If the previous day is a weekday, return the previous day / weekday. Otherwise return the previous Friday since, if the previous day is a Saturday or Sunday it should return the previous Friday.

If you need to check only for Saturday or Sunday, this snippet can be used. I'm getting last day of month first then checking for business day (so basically last business day of a month)

getLastDayofMonth() {
      let date = moment(this.selectedDate).endOf("month").format("MM-DD-YYYY");
      // console.log(moment(date).day());
      // check if last day of selected month is Saturday or Sunday, assign last friday if true
      if (moment(date).day() === 0 || moment(date).day() === 6)
        this.selectedDate = moment(date)
          .subtract(6, "days")
          .day(5)
          .format("MM-DD-YYYY");
      else this.selectedDate = date;
    },

发布评论

评论列表(0)

  1. 暂无评论