0

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?

j08691
  • 204,283
  • 31
  • 260
  • 272
user9114945
  • 289
  • 3
  • 14

1 Answers1

3

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.

CherryDT
  • 25,571
  • 5
  • 49
  • 74