convert pacific time to eastern time in javascript

To convert Pacific time to Eastern time in JavaScript, you can use the toLocaleString() method of the Date object with the timeZone option. Here's an example:

index.tsx
// create a date object for a specific moment in Pacific time
const pacificTime = new Date('2022-01-01T12:00:00-08:00');

// convert to Eastern time
const easternTime = pacificTime.toLocaleString('en-US', { timeZone: 'America/New_York' });

console.log(easternTime); // output: "1/1/2022, 3:00:00 PM"
301 chars
8 lines

In this example, we create a Date object for January 1, 2022 at 12:00:00 PM Pacific time (which is 3 hours behind Eastern time). We then use the toLocaleString() method to format the date object as a string in the en-US locale and specify the 'America/New_York' timezone option to convert to Eastern time.

The output shows the date and time in Eastern time: "1/1/2022, 3:00:00 PM".

gistlibby LogSnag