# Formula to set a duration (DateTimeRange)

**URL:** <https://community.fibery.io/t/formula-to-set-a-duration-datetimerange/2384>\
**Category:** Get Help\
**Created:** [January 13, 2022, 6:26pm UTC](https://community.fibery.io/t/formula-to-set-a-duration-datetimerange/2384 "2022-01-13T18:26:31Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![uniquelau](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/uniquelau/32/3836_2.png) [@uniquelau](https://community.fibery.io/u/uniquelau)\
**Post date:** [January 13, 2022, 6:26pm UTC](https://community.fibery.io/t/formula-to-set-a-duration-datetimerange/2384/1 "2022-01-13T18:26:31Z")

</div>

Hello fellow friendly fibery folk,

In short - is it possible to easily manipulate date times using formulas, e.g. set / add / remove units of time.

Pain: “Setting a duration involves lots of clicks”  
Idea: “Create a button that does some the hard work”

E.g. Duration = 30 minutes, Duration = 1 hour

So for a DURATION field, I need to set a sensible DateTimeRange.

I got as far as:

`DateTimeRange(Now(), Now())`

Which does what you’d expect:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/f/fe19ddb405a73b75164039d21212b6d28be7c06b.png)

However, what I’d actually like to do is!

1. Get the current date
2. Extract the current time
3. Round the time down to a nearest interval (e.g. 15 minutes)
4. Create a time range using the rounded down internal as the start
5. Send the end range to the start plus X minutes

e.g.

current datetime = 2020-01-13-20-23-12  
rounded to nearest interval = 2020-01-13-20-15-00

start = rounded to nearest interval  
end = start + 30 minutes

Any ideas much appreciated 👍

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 14, 2022, 12:42am UTC](https://community.fibery.io/t/formula-to-set-a-duration-datetimerange/2384/2 "2022-01-14T00:42:31Z")

</div>

Create an integer field called ‘Surplus’.  
Then make a button with the following actions:

 ![image](https://us1.discourse-cdn.com/flex020/uploads/fibery/original/2X/7/778b3f4eb0672aa42a2b5089817f3d2f2478b32a.png)

The formulas are as follows:

`Minute(Now()) - (RoundDown(Minute(Now()) / 15,0) * 15)`

and

`DateTimeRange(Now() - Minutes([Step 1 Meeting].Surplus),Now() - Minutes([Step 1 Meeting].Surplus) + Minutes(30))`

assuming that your database is called Meeting 🙂

---

<div class="post-metadata">

**Author:** ![uniquelau](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/uniquelau/32/3836_2.png) [@uniquelau](https://community.fibery.io/u/uniquelau)\
**Post date:** [January 19, 2022, 12:57pm UTC](https://community.fibery.io/t/formula-to-set-a-duration-datetimerange/2384/3 "2022-01-19T12:57:18Z")

</div>

Hi Chris,

Apologies for my delay replying to this, I’ve been mulling it over!

I am going to try and implement something along these lines.

The main point I’ve been thinking about is this. Is there any way to have a locally scoped variable (e.g. within the formula)? It seems a bit brittle to add additional fields for the purpose of these calculations, especially as there no easy way to understand what those fields are used for (e.g. if they are being used at all).

My fear is creating something that I won’t understand in 18 months time.

A locally scoped variable would keep everything in one place.

Thanks again for your suggestion it’s helpful.

/Laurence

---

<div class="post-metadata">

**Author:** ![Chr1sG](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/chr1sg/32/3941_2.png) [@Chr1sG](https://community.fibery.io/u/Chr1sG)\
**Post date:** [January 19, 2022, 2:36pm UTC](https://community.fibery.io/t/formula-to-set-a-duration-datetimerange/2384/4 "2022-01-19T14:36:00Z")

</div>

It’s a fair point, and you’re not the first to recognise a need:

> [@Variables in formulas](https://community.fibery.io/t/variables-in-formulas/1277):
>
> Hi, I have been writing some quite advanced formulas in Fibery: I found that after some time even I cannot figure out what the heck formula does as it’s all in one gigantic expression. Would it be possible to add support for variables so that formulas can stay readable overtime? My only workaround now is to create a bunch of other formula fields and use them instead. However, that creates a lot of unnecessary noise in the type structure.

---

<div class="post-metadata">

**Author:** ![Marloes](https://sea2.discourse-cdn.com/flex020/user_avatar/community.fibery.io/marloes/32/5926_2.png) [@Marloes](https://community.fibery.io/u/Marloes)\
**Post date:** [January 12, 2023, 9:03am UTC](https://community.fibery.io/t/formula-to-set-a-duration-datetimerange/2384/5 "2023-01-12T09:03:58Z")

</div>

Untill there is a different solution, maybe Chat GPT is able to help you. Chat GPT is an AI software you can chat with.

I’ve found is usefull to add a formula there when I don’t remember what is does with the question “Can you explain what this formula does” and than Chat GPT will explain it.
