PDT offset is -7 or -8 hours depending on daylight saving time. I cannot simply subtract 7 * 60 * 60 from the UTC timestamp, as everybody else seems to be suggesting when googling this issue.

How can I simply alert(); the current current PDT time using Javascript?

4

1 Answer

You can use toLocaleString (there is also toLocaleTimeString if you don't need the date):

alert(new Date('2020-01-01T00:00:00Z').toLocaleString(undefined, { timeZone: 'America/Los_Angeles' })) // Output: 12/31/2019, 4:00:00 PM 

This example uses a fixed time to show the relation between input and output. To use the current time, just use new Date() without initial value.

The first parameter is the locale. The undefined here means that it's the current system locale, so the format (not the timezone) will be based on the user's settings (for example, on my machine it outputs 31.12.2019, 16:00:00). You can also set a fixed locale there if you want, for example en-US.

1

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy