JayJona
JayJona

Reputation: 502

Get date in a specific timezone

i don't live in the pacific area and i am looking for a way to get the current day in the pacific area as my midnight does not match pacific midnight, how to do?

Current code

//how to set this to Pacific Time?
var timezone = new Date();

//get the current day
var day = timezone.getDate();

//use the day           
var row_for_day = day+3;

Upvotes: 0

Views: 765

Answers (1)

user5734311
user5734311

Reputation:

Here's one way to do this, using Date#toLocaleString():

(target timezone name can be looked up here: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones)

const target = "America/Los_Angeles";

const laDate = +new Date().toLocaleString("en-US", {
  timeZone: target,  // conversion here
  day: "numeric"     // return day only
});

console.log("date in LA:", laDate);

I was looking for existing answers but wasn't able to find a good, current, concise fit. Point one out and I will remove this.

Upvotes: 2

Related Questions