skip to Main Content

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


  1. As long as you know the destination timezone (PDT vs PST, etc..), then you can just specify the timezone in your Date constructor:

    let date = "13 Dec 2025"
    let targetTime = "09:30:00 PDT"
    
    let localMs  = new Date( )
    let targetMs = new Date( `${date} ${targetTime}` )
    
    let diff = targetMs - localMs
    
    console.log( `Diff : ${diff} ms` )
    console.log( `Diff : ${diff / 1000} secs` )
    console.log( `Diff : ${diff / 1000 / 60} mins` )
    console.log( `Diff : ${diff / 1000 / 60 / 60} hours` )
    Login or Signup to reply.
  2. You can use toLocalString with Etc/GMT+8 to fix the timezone without savings time, and then set the hours and minutes after:

    
    // this gives you a timestamp for the given date at midnight in GMT+8
    const pstDate = new Date(
      new Date(new Date('12/13/2025').toLocaleString('en-US', { timeZone: 'Etc/GMT+8' }))
    );
    
    // now add 9:30 on top of that if you want that time in GMT+8
    pstDate.setHours(
      new Date('12/13/2025').getHours() + 9,
      new Date('12/13/2025').getMinutes() + 30
    );
    
    console.log(pstDate - new Date());
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search