# Adding duration to date and time using Javascript

**URL:** <https://community.glideapps.com/t/adding-duration-to-date-and-time-using-javascript/66463>\
**Category:** Ask for Help\
**Created:** [September 23, 2023, 6:02pm UTC](https://community.glideapps.com/t/adding-duration-to-date-and-time-using-javascript/66463 "2023-09-23T18:02:06Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Abhishek\_Sinha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/abhishek_sinha/32/52361_2.png) [@Abhishek\_Sinha](https://community.glideapps.com/u/Abhishek_Sinha)\
**Post date:** [September 23, 2023, 6:02pm UTC](https://community.glideapps.com/t/adding-duration-to-date-and-time-using-javascript/66463/1 "2023-09-23T18:02:06Z")

</div>

I get a json response with a value of date and time. For example - 2023-09-25T18:00:00.000Z  
I want to add a duration of x hours to this for example - add 3.5 hours to this.  
In the glide table simple math column work if the column is set to date.  
In this case it doesn’t work.

Can anyone help with a Javascript code to do this ?

---

<div class="post-metadata">

**Author:** ![slscustom.ru](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/slscustom.ru/32/77336_2.png) [@slscustom.ru](https://community.glideapps.com/u/slscustom.ru)\
**Post date:** [September 23, 2023, 6:33pm UTC](https://community.glideapps.com/t/adding-duration-to-date-and-time-using-javascript/66463/2 "2023-09-23T18:33:20Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/f/6/f6cd0ab91105977bf3517ecf4f00134043ce6744.jpeg)

```auto
// Original date
const originalDate = new Date(p1);

// Number of hours to add
const hoursToAdd = (p2); // You can change this to any number of hours you want to add

// Create a new date by adding hours
const newDate = new Date(originalDate);
newDate.setHours(newDate.getHours() + hoursToAdd);

// Display the result
return(newDate.toISOString());

```

---

<div class="post-metadata">

**Author:** ![Abhishek\_Sinha](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/abhishek_sinha/32/52361_2.png) [@Abhishek\_Sinha](https://community.glideapps.com/u/Abhishek_Sinha)\
**Post date:** [September 23, 2023, 7:02pm UTC](https://community.glideapps.com/t/adding-duration-to-date-and-time-using-javascript/66463/3 "2023-09-23T19:02:39Z")

</div>

Perfect, thanks it works. Is there a way to add decimal of hours too ? This takes rounded value of hours even if p2 is a decimal.

---

<div class="post-metadata">

**Author:** ![slscustom.ru](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/slscustom.ru/32/77336_2.png) [@slscustom.ru](https://community.glideapps.com/u/slscustom.ru)\
**Post date:** [September 23, 2023, 7:47pm UTC](https://community.glideapps.com/t/adding-duration-to-date-and-time-using-javascript/66463/4 "2023-09-23T19:47:01Z")

</div>

![image](https://us1.discourse-cdn.com/flex002/uploads/glideapps/original/3X/3/2/326f5679cb49e0905a2337826bb79cfa24002cdf.png)

```auto
// Original date
const originalDate = new Date(p1);

// Decimal number of hours to add
const hoursToAdd = p2;

// Calculate the milliseconds equivalent of the decimal hours
const millisecondsToAdd = hoursToAdd * 60 * 60 * 1000;

// Create a new date by adding milliseconds
const newDate = new Date(originalDate.getTime() + millisecondsToAdd);

// Display the result
return(newDate.toISOString());

```

---

<div class="post-metadata">

**Author:** ![system](https://sea2.discourse-cdn.com/flex002/user_avatar/community.glideapps.com/system/32/53398_2.png) [@system](https://community.glideapps.com/u/system)\
**Post date:** [September 30, 2023, 7:47pm UTC](https://community.glideapps.com/t/adding-duration-to-date-and-time-using-javascript/66463/5 "2023-09-30T19:47:44Z")

</div>

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