# How to convert decimal to duration?

**URL:** https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923
**Category:** Ask for Help
**Created:** [June 10, 2022, 7:24am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923 "2022-06-10T07:24:00Z")
**Posts on this page:** 20
**Page:** 1

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 7:24am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/1 "2022-06-10T07:24:00Z")

</div>

How do i convert a decimal to a duration format?

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [June 10, 2022, 8:02am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/2 "2022-06-10T08:02:34Z")

</div>

Do you mean that you have a number representing decimal hours (eg. 5.3) and you want to represent it as hh:mm?

First thing to note is that if you use a Glide math column to subtract one date/time from another, then it will give you a duration format by default. So this is the easiest and most direct way to get there.

But if you’re not starting with two dates and only have a decimal number, then you can convert it with math columns to separately extract the HH/MM components, and then stitch them together with a template column.

To get the hours: `Trunc(Round(Hours,2))` (where Hours is your decimal hours)  
To get the minutes: `Round((Hours-WholeHours)*600/10,0)` (where WholeHours is the result of the first column)

 ![Screen Shot 2022-06-10 at 4.01.51 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/1/3129b1a3b82a2a40b5bd494bcd810cde486a9300.png)

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 9:15am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/3 "2022-06-10T09:15:03Z")

</div>

> [@Darren\_Murphy](#):
>
> Trunc(Round(Hours,2))

Wow support is quick.

Will this work if I am using a Math formula to calculate the decimal duration?

 ![tripduration-formula](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/7/4/748914d7dcd18c0acae0023bc717a8c712734b91.png)

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 9:24am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/4 "2022-06-10T09:24:07Z")

</div>

Thank you I got it to work. How would I calculate the seconds?

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 9:35am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/5 "2022-06-10T09:35:31Z")

</div>

The final hurdle is to add the duration to the DateTime? I am not sure how to do that. Any help would be appreciated.

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [June 10, 2022, 9:46am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/6 "2022-06-10T09:46:43Z")

</div>

To get seconds as well, firstly adjust the formula for minutes as follows:

`Trunc((Hours-WholeHours)*600/10,0)` (uses trunc instead of round, so we don’t discard the remainder)

And then use the following to get the remaining seconds:

`(Round((Hours-WholeHours)*600/10,2)-Minutes)*60`

To add decimal hours to a date/time, it’s just a simple math formula: `Date + (Hours / 24)`

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 10:50am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/7 "2022-06-10T10:50:58Z")

</div>

Perfect, I have it working correctly. Thank you Darren.

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 11:04am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/8 "2022-06-10T11:04:34Z")

</div>

> [@Darren\_Murphy](#):
>
> Round((Hours-WholeHours)\*600/10,2)-Minutes)\*60

Can you confirm if the formula for seconds is correct? I am getting a zero value. I assume I add the seconds formula to a new math column?

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [June 10, 2022, 11:15am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/9 "2022-06-10T11:15:28Z")

</div>

hmm, I might have messed that up. Let me double check and I’ll get back to you.

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 11:21am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/10 "2022-06-10T11:21:27Z")

</div>

Thanks

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [June 10, 2022, 11:30am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/11 "2022-06-10T11:30:00Z")

</div>

It gives the correct results for me.  
One problem you’ll have is if you want leading zeroes - eg. 1:03:05 instead of 1:3:5, then you’ll need extra columns to apply that formatting.

To be honest, this is a case where I would probably turn to a single Javascript column and save all that mucking about. Try the following in a JS Code column:

```auto
var d = new Date(0,0);
d.setSeconds(p1 * 60 * 60);
return d.toTimeString().slice(0,8);

```

 ![Screen Shot 2022-06-10 at 7.26.36 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/1/7/17e469ff283c4edac5a5407957688a1db58f418a.png)

![Screen Shot 2022-06-10 at 7.26.25 PM](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/6/a/6a63c8c53f87c5ec31cabf4e09cd93c07e8fb7d5.png)

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 11:47am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/12 "2022-06-10T11:47:15Z")

</div>

> [@Darren\_Murphy](#):
>
> ```auto
> var d = new Date(0,0);
> d.setSeconds(p1 * 60 * 60);
> return d.toTimeString().slice(0,8);
> 
> ```

I have tried that but get an Invalid Column Value

 ![js-duration](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/a/6/a6b1a7af059b0705208dc51f695fb4cb8e138f43.png)

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 11:48am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/13 "2022-06-10T11:48:45Z")

</div>

Nevermind, figured out the issue. It’s working now.

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [June 10, 2022, 11:51am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/14 "2022-06-10T11:51:46Z")

</div>

yeah, you would get that if the Hours column is empty.  
Simple fix is to insert a bit of code to short circuit when it’s not defined, something like this:

`if (p1 === undefined) { return undefined }`

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 12:03pm UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/15 "2022-06-10T12:03:05Z")

</div>

Sorry I know this is off topic but is it normal for the Set Column Values to not add all the data to a new row in Google Sheets when you add new data but only when you edit and then click done?

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [June 10, 2022, 12:07pm UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/16 "2022-06-10T12:07:19Z")

</div>

I’m not sure I understand your question. Set Column values isn’t required when you edit a record. Any changes that you make should be saved once you click on done. You should see the changes in the underlying Glide Table immediately, but it may take a short while before the change appears in a connected Google Sheet. Does that answer the question?

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 10, 2022, 12:43pm UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/17 "2022-06-10T12:43:58Z")

</div>

I have a trip cost screen, when I hit the + to add a new record, I use the Set Column Values to set the column Values of my google sheet from glide’s computed columns. But that does not happen. The column values are left blank after adding. When I click to edit the new row I have to use use Set column values as a workaround to write the missing column values back to my Google Sheets.

The data is never written to the columns on add no matter how long a wait.

This issue appears to be around since 2020, after going through a few forum posts.

 ![trip-cost-screen](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/8/9/897268ae2fb8ff2d713c3400843b54e93925307c.png)  
 ![trip-cost-add](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/1/5/152a57fa4dc60cf22403754522c6462e12070982.jpeg)

 ![blank-sheets-columns](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/1/e/1edfd2b3579cc7e53653c23fdaf2545d37150600.png)

---

<div class="post-metadata">

### Author: ![Darren\_Murphy](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/darren_murphy/32/47326_2.png) [@Darren\_Murphy](https://community.glideapps.com/u/Darren_Murphy)
#### Post date: [June 10, 2022, 12:48pm UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/18 "2022-06-10T12:48:56Z")

</div>

ah, I see.  
My guess would be that you might have a chicken and egg situation. That is, the Set Column Values happens before the values are calculated. This might work better with a [Custom Form](https://community.glideapps.com/t/how-do-i-create-a-custom-form/25014).

Curious about why you need these values in the Google Sheet? Are they used outside your app?

---

<div class="post-metadata">

### Author: ![gvalero](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/gvalero/32/1037_2.png) [@gvalero](https://community.glideapps.com/u/gvalero)
#### Post date: [June 10, 2022, 2:28pm UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/19 "2022-06-10T14:28:37Z")

</div>

Hola,

Maybe a little late but it can be useful in future.

What if we use the **Duration** column that does it automatically (here thinking about saving columns/data) 😇 ?

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/0/b/0bcbfd93c46ef9e1633a114099b2d8eae320d296.png)

Just:

1. Take the time as number
2. Convert it to hours (time/24)
3. Write result (time/24) to Duration column
4. Enjoy Glide

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/c/0/c06a7f853857a4a6d2e3f3ba7183bbb07ca9d87b.png)

Saludos!

---

<div class="post-metadata">

### Author: ![Guilio\_Del\_Fava](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/guilio_del_fava/32/65469_2.png) [@Guilio\_Del\_Fava](https://community.glideapps.com/u/Guilio_Del_Fava)
#### Post date: [June 11, 2022, 9:33am UTC](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923/20 "2022-06-11T09:33:43Z")

</div>

Hi, thanks for the feedback. How do you write the Time to a Duration Column?

[Next page](https://community.glideapps.com/t/how-to-convert-decimal-to-duration/42923.md?page=2)
