How to configure a dateadd / weekday formula to return a time only within specified business hours

Hello,

Is there a way to configure this formula to only return a time during specified business hours (7:30-4PM)?

DATEADD(WORKDAY({Last Modified},1))

Thanks :slight_smile:

Hi @Aysia_Saylor,

could you describe more the issue you want t o solve?

In a first view I get that you want the date of specific date fact. Is that right ?

Thanks

@Dimitris_Goudis As I read it, @Aysia_Saylor wants to take the date from the {Last Modified} field, add one workday to it, and then ensure that the time portion of the result falls within specific hours of a day.

@Aysia_Saylor It’s actually a little easier to target whole hours and ignore minutes, so I set this up to keep the new time within the range of 8 am to 4 pm:

DATEADD(
    WORKDAY({Last Modified}, 1),
    MAX(MIN(HOUR({Last Modified}), 15), 8) - HOUR({Last Modified}),
    "hours"
)

The HOUR() function returns the hour of a given datetime in 24-hour format (specifically 0-23, with 0 being midnight). The nested MAX() and MIN() functions take that hour and force it to be between 8 (8 am) and 15 (3 pm1). We then take that result and subtract the original hour, which gives us an offset value to use in DATEADD(). For example, if the original hour is 1 (1 am), it’s forced to 8; 8 - 1 = 7, meaning we need to add 7 hours. On the other end, if the original hour is 22 (10 pm), it’s capped to 15 (3 pm). 15 - 22 = -7, meaning we need to subtract 7 hours. All hours between 8 and 15 return an offset of 0, so they’re left unmodified.

Now, this will only work as written if you live in GMT because dates are stored internally relative to GMT. Even if the {Last Modified} field is set to display local time, the HOUR() function will give you the GMT hour because it’s operating on the raw data, not the displayed version. To force it to reflect your local timezone, you’ll need to tell Airtable to recalculate the time using SET_TIMEZONE() before the HOUR() function gets it:

DATEADD(
    WORKDAY({Last Modified}, 1),
    MAX(MIN(HOUR(SET_TIMEZONE({Last Modified}, "America/Los_Angeles")), 15), 8)
    - HOUR(SET_TIMEZONE({Last Modified}, "America/Los_Angeles")),
    "hours"
)

I used “America/Los_Angeles” for the timezone because that’s the zone in which I live. Change that to the appropriate timezone indicator for your region.


1 The hour caps a 3 pm because we’re only checking the hour, not the minutes. If the minutes are above 0, capping at 4 pm would mean a time like 4:03 might get through. Capping at 3 pm means that all times will be before 4 pm, not after.

2 Likes

Hi @Justin_Barrett,
thanks for the solution and the clear explanation. At the beginning I didn’t get the issue like this.

Thanks

Thank you Justin! This works great. I appreciate you explaining the reasoning behind it too so I can replicate that logic in the future when creating other formulas.

This topic was solved and automatically closed 3 days after the last reply. New replies are no longer allowed.