convert eastern time to pacific time in javascript

To convert Eastern Time to Pacific Time in JavaScript, you can use the Moment.js library. Here's how you can do it:

index.tsx
// set the date and time in Eastern Time
let date = moment.tz("2021-10-20 08:00:00", "America/New_York");

// convert to Pacific Time
date = date.clone().tz("America/Los_Angeles");

// format the date and time in Pacific Time
const formattedDate = date.format("YYYY-MM-DD HH:mm:ss");

console.log(formattedDate); // outputs "2021-10-20 05:00:00"
346 chars
11 lines

In this code, we first set the date and time in Eastern Time using the moment.tz() function. We pass in the date string as the first argument and the timezone string (in this case, "America/New_York") as the second argument.

We then clone this moment object and convert it to Pacific Time using the tz() function again with the timezone string "America/Los_Angeles".

Finally, we format the resulting moment object as a string using the format() function and output the formatted date and time to the console.

Note that you'll need to include the Moment.js library in your project and import it into your JavaScript file before you can use it.

gistlibby LogSnag