Getting data from remote API with timestamp as string in format ""2024-09-25T00:00:00.000". This is in local time with summertime adjustment. I need to pass this data to a database in normal UTC format. How to make that adjustment? Preferably native javascript, otherwise moment.js might be the solution.
Because all results I find mention "+XX:00" string at the end to adjust for timezone. But I don't see how that takes summertime into effect. I could possibly get the timezone offset in minutes and adjust from there. But this data arrives with 24 hours for a full day and processed once per day, so worried how that offset will affect on the day of summertime adjustment (twice per year). What a minefield time is.
Naughty, broken API! It should at least include a timezone indicator. Better still, it should send in UTC to avoid DST and timezone switch problems.
Assuming you know the timezone, you could manually add the tz indicator to the end of that string and then do a new Date(apiDate) on it. I believe Node-RED also now makes momentjs available in function nodes so you could use that as well (more efficient than using JSONata).
Yeah was surprised by the API indeed! All examples I find use stuff like "2025-09-25 09:00+08:00" but that doesn't work with summertime where the offset changes 2 times a year. Hopefully moment.js is built-in. Because as far as I can see, javascript date doesn't accept timezone argument for input string, only for output string.
Your source date is an ISO string - I forgot that you can't use a timezone standard 2 or 3 character designator, only an offset. So you either have to use momentjs or make use of the INTL library which will let you work with localised date/time values.
I do hope that you don't need to deal with times that span the DST switchover (usually 2am I think), because that can cause serious confusion if not actual data issues.
Moment.js is built-in! Only needed to add to function setup module. But not sure if I need additional moment-timezone package? This flow runs once per day to handle all 24 hours of yesterday so will crash straight into that DST switchover haha.
Edit: moment-timezone is built-in also! Fantastic
This was a solution suggested by chatgpt and initial tests seem to verify it works:
// Original date string
const dateString = "2024-09-25T00:00:00.000";
// Parse the date string in the "Europe/Oslo" time zone
const osloDate = moment.tz(dateString, 'Europe/Oslo');
// Convert to UTC
const utcDate = osloDate.utc();
// Display the result
console.log("UTC Date:", utcDate.format()); // Outputs in ISO 8601 format
I suspect it will handle most of the day of the DST switchover, particularly hours before and after 02:00/03:00. But I'm sure it will be wrong at that one or two hours of switching. It will have to be acceptable data corruption.