Create new datetime that is datetime + 1 hour from object

I pull data from an API for dynamic energy tarrifs. Every hour has a startsAt-property in this format: 2024-10-18T10:00:00.000+02:00

But I am missing the end time. I want to create calendar events from these objects to trigger a few energy related things. I have been breaking my mind about this and searching for a very long time now to get this done in a lambda when creating the event by filling in the end time as “start time + 1hr”. But I have not been able to find anything. At least nothing that was an option to include in one row of jsonata with lambda. Also, documentation on jsonata on time objects is very limited so I assume the options are too. Is there any way or are my assumptions correct?

Another option I could think off is to create a function that for every object creates a new property endsAt which value is “startsAt + 1hr”. Then I can fully use JavaScript to first parse the datetime string, then add 1 hour (add 3600s to epoch value) and then create a datetime string, again with offset (which also seems to be a bit of a challenge if one wants that time offset included). Then add the value to the new property.

Something else would be to just replace part of the string. Very ugly if you ask me and maybe prone to error. But every datetime entry has the exact same amount of characters and every element (hour, minute, second and so on) is in exactly the same index everytime. So I could just add 59 minutes and 59 seconds to replace the 00:00 in the string. Should work but doesn’t feel right.

I am wondering what your ideas and opinions would be for the mentioned options. Has anyone got any other idea? I am trying to find the cleanest, simplest and easiest to understand option. I have been thinking and searching a lot but delay in my project is starting to become too much for this seemingly simple problem…

Example using JSONata with the moment library.

$moment("2024-10-18T10:00:00.000+02:00").add(1, 'hour')
1 Like

I found the moment library but I though I had to declare/import it. If I understand correctly that is not necessary? That will make things sooooo much easier, for other stuff too. I thought I’d have to use a function to import and use it. This will make my flow much cleaner, shorter and more efficient.

I will try the following when I got time to:

$moment($Object.startsAt).add(1, 'hour')

Just did a little test. Adding the hours work. The string format changes however. I will look up moment.js documentation and see if I can get the same string format as the source.

Adding .format() did the trick :smiley:

$moment($Object.startsAt).add(1, 'hour').format()