Struggling with Time Zone Conversions

I’ve been on a journey trying to find something that will help me get the UTC offset based on an IANA time zone and date object.

I’ve experimented with Moment Timezone (see issue below), DAYJS (known bug with creating a date object in a time zone), and Luxon (needing support for Intl and other dependencies not easily supported in Postman) and am back to Moment Timezone.

The problem I’m having with Moment is that I cannot get a correct UTC offset based on Daylight Savings Time. I will know the date object and IANA time zone at the time of creation, so I’m trying to establish that Date object in the time zone, rather than trying to figure out what the conversion to my local time would be, back to the conversion based on the IANA time zone.

The problem is that I cannot seem to get a correct UTC offset based on whether Daylight Savings is in effect.

At this stage, I’m willing to either be redirected on how I’m creating/getting the UTC offset, or I’m open to other suggestions outside of DAYJS and Luxon that others have hopefully used with success:

if (!pm.globals.has("moment_js") || !pm.globals.has("moment_tz")) {
    pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/moment-timezone/0.5.45/moment-timezone-with-data-10-year-range.js", (mtzErr, mtzRes) => {
        pm.sendRequest("https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.30.1/moment.min.js", (mjsErr, mjsRes) => {
            pm.globals.set("moment_js", mjsRes.text());
            pm.globals.set("moment_tz", mtzRes.text());
        })
    })
}

(new Function(pm.globals.get("moment_js")))();
(new Function(pm.globals.get("moment_tz")))();

console.log(moment.tz("2024-06-26T16:55:00", "America/New_York").format("YYYY-MM-DDThh:mm:ssZZ"));
    //Returns 2024-06-26T04:55:00-0500, but the offset is wrong and should be "0400"

console.log(moment.tz("2024-12-26T16:55:00", "America/New_York").format("YYYY-MM-DDThh:mm:ssZZ"));
    //Returns 2024-12-26T04:55:00-0500 which is correct

I think its the copy of the timezone library you are using, as I had the same issue with the code you provided.

I then updated the code to pull it directly from the downloads on the moment website (which appears to be exactly the same files BTW and it the started working).

So I recommend deleting the global variables you currently have, and get it to pick them up again.

if (!pm.globals.has("moment_js") || !pm.globals.has("moment_tz")) {
    pm.sendRequest("https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.js", (mtzErr, mtzRes) => {
        pm.sendRequest("https://momentjs.com/downloads/moment.js", (mjsErr, mjsRes) => {
            pm.globals.set("moment_js", mjsRes.text());
            pm.globals.set("moment_tz", mtzRes.text());
        })
    })
}

(new Function(pm.globals.get("moment_js")))();
(new Function(pm.globals.get("moment_tz")))();

console.log(moment.tz("2024-06-26T16:55:00", "America/New_York").format("YYYY-MM-DDTHH:mm:ss ZZ"));
console.log(moment.tz("2024-12-26T16:55:00", "America/New_York").format("YYYY-MM-DDTHH:mm:ss ZZ"));

2 Likes

Mike Jones to the rescue, again! This resolved the issue. Thank you very much!

1 Like

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