Add days after date is selected excluding weekends and holidays

前端 未结 3 1932
半阙折子戏
半阙折子戏 2021-01-06 10:28

I have two text fields. In the first textfield I have added a calendar using jQueryUI Datepicker. When the user selects the date from the datepicker, it should automatically

3条回答
  •  时光取名叫无心
    2021-01-06 11:04

    When dealing with holidays, there's really only one option: one day at a time. You should iterate, one day at a time, and add/substract as needed if a day "counts". In pseudo code:

    date add(date startDate, int daysToAdd) {
        int i:=0
        endDate:=startDate
        while (i

    isWeekend() is trivial to implement; isHoliday(), on the other hand, is a very tough nut to crack. The easiest way to deal with it is to have a table of known holidays and check if the date passed as parameter coincides with any of those. In my opinion, it's better to have a rule-based method that can compute whether a given date is a holiday or not.

    Disclaimer: I copied and pasted this from a previous answer of mine to the same question. See How to ignore weekends and holidays in boost date time?

提交回复
热议问题