I’d like to find the time offset to a particular time (let’s say 9:30am) on a particular date in Pacific Time, whereas my program may be running in some other locale. This won’t work:
const targetdate = '12/13/2025';
const target = new Date(targetdate);
target.setHours(9, 30); // won't work, uses local time zone
const timeDelta = target - new Date();
because setHours
uses the local time zone rather than Pacific.
The following won’t work half the year either:
const PST_offset = 7; // won't work if target date is DST
target.setUTCHours(9 + PST_offset, 30);
because the offset from UTC is different during daylight savings time.
I’m not sure how to go about this. Is there some way to tell Node to use Pacific as its locale? I’m aware of Intl.DateTimeFormat
too but that has to do with displaying dates & times, not Date
math.
2
Answers
As long as you know the destination timezone (PDT vs PST, etc..), then you can just specify the timezone in your Date constructor:
You can use
toLocalString
withEtc/GMT+8
to fix the timezone without savings time, and then set the hours and minutes after: