# Script for offsetting dates by business/working days

**URL:** <https://community.fibery.io/t/script-for-offsetting-dates-by-business-working-days/3231>\
**Category:** API & Programming\
**Created:** [September 1, 2022, 4:18pm UTC](https://community.fibery.io/t/script-for-offsetting-dates-by-business-working-days/3231 "2022-09-01T16:18:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dimitri\_S](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/dimitri_s/32/1683_2.png) [@Dimitri\_S](https://community.fibery.io/u/Dimitri_S)\
**Post date:** [September 1, 2022, 4:18pm UTC](https://community.fibery.io/t/script-for-offsetting-dates-by-business-working-days/3231/1 "2022-09-01T16:18:18Z")

</div>

Getting notifications or having tasks become due on weekends isn’t very useful. And as far as I can tell there’s no way to do this via a formula so here is a pretty simple script to add **working/business** days to a date. Meaning:

- Thursday + 1 business day = Friday
- Friday + 1 business day = Monday
- Saturday & Sunday + 1 business day = Monday
- Monday + 1 business day = Tuesday

```auto
function addBizDays(startDate, bizDays) {
    for (let i = 1; i <= bizDays; i++) {
        startDate.setDate(startDate.getDate() + 1);
        if (startDate.getDay() === 6) {
            startDate.setDate(startDate.getDate() + 2);
        }
        else if (startDate.getDay() === 0) {
            startDate.setDate(startDate.getDate() + 1);
        }
    }
    return new Date(startDate);
}

const fibery = context.getService('fibery');
for (const entity of args.currentEntities) {
    // an entity contains all fields apart from collections, to access a field refer to it by its UI name
    const startDate = new Date(entity['Start Date']);
    const businessDays = entity['Add Biz Days'];
    // to update an entity provide an object with the new values
    await fibery.updateEntity(entity.type, entity.id, {
        'Result Date': addBizDays(startDate, businessDays).toISOString().split('T')[0],
    });
}

```

So in the above example `Start Date` is a DB date field that is the input date, `Add Biz Days` is the DB field that holds the number of days to offset the Start Date by, and `Result Date` is the output date field.

If you want the start date to always be today just do this:

```auto
const startDate = new Date();

```

If you want the offset days to be a fixed value just do something like this:

```auto
const businessDays = 2;

```

---

<div class="post-metadata">

**Author:** ![Matt\_Blais](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/matt_blais/32/1464_2.png) [@Matt\_Blais](https://community.fibery.io/u/Matt_Blais)\
**Post date:** [September 1, 2022, 4:43pm UTC](https://community.fibery.io/t/script-for-offsetting-dates-by-business-working-days/3231/2 "2022-09-01T16:43:33Z")

</div>

This is another good example of why it would be useful to have some sort of Workspace library/module support for scripts.
