To convert Pacific time to Eastern time in Typescript, you can use either the moment.js
or date-fns
library. Here is an example using moment.js
:
index.ts336 chars8 lines
In this example, we first create a moment
object representing the Pacific time with the America/Los_Angeles
time zone. Then we clone the moment
object and convert it to the Eastern time zone (America/New_York
) using the tz()
method. Finally, we format the Eastern time and display both times in the console.
Alternatively, here is an example using date-fns
:
index.ts326 chars8 lines
In this example, we first define the Pacific time as a string. Then we use the utcToZonedTime()
function from the date-fns-tz
library to convert the Pacific time to a Date
object in the Pacific time zone. We then format the Eastern time as a string using the format()
function and passing in the Date
object and the America/New_York
time zone. The timeZone
option specifies the time zone to use for formatting the date. Finally, we display both times in the console.
gistlibby LogSnag