So I may be looking at this entirely wrong, but essentially I need to add x amount of hours to a JavaScript date which i’ve done using this function:
export function addHoursToDate(hoursFromNow: number = 5)
{
return new Date(new Date().setTime(new Date().getTime() + (hoursFromNow*60*60*1000)));
}
(This is based on an answer I saw on this site as well) and that does work fine, the problem is I need to convert it to this format:
"2023-10-25T15:00:00.000Z"
(for example) which I understand is the standard date/time format. Usually I use toIsoString()
problem is when I do that it converts it to UTC.
Essentially I need to be able to add x/y hours/minutes to the date but have it in the datetime format as above? Im sure im missing something obvious but I wanted to see if there was a clean way to do this. But also maybe im thinking about it the wrong way as well.
2
Answers
Trivial if the output stays ISO
From now
You can use moment.js (https://momentjs.com/) to achieve the same what you are looking for,
Here is how you do it using momentjs library
Check out the official documentation for more in depth details – https://momentjscom.readthedocs.io/en/latest/moment/03-manipulating/01-add/