Adding duration to date and time using Javascript

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 ?

// 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());

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.

// 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());

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