pixie123 pixie123 - 2 months ago 17
CSS Question

Detect Pacific timezone with Javascript

I want to do a if statment if the current time in pacific time is 11am. how can i do that? here is what i have right now:

var time = new Date().getHours();

if(time == 11) {
alert("this works");
}


but that only detects the user's time. how can i do pacific time only?

Answer

Combination of getTimezoneOffset() and PST (-7), currently Pacific Daylight Time!

var offset = new Date().getTimezoneOffset() / 60;
var localHour = new Date().getHours();
var PSTHour = localHour + offset - 7;
PSTHour = PSTHour > 0 ? PSTHour : PSTHour + 24;
console.log('PST hour: ' + PSTHour);

Comments