Home > Enterprise >  How would I take a start/end dates user input, and convert it to US Pacific time, in ISO format?
How would I take a start/end dates user input, and convert it to US Pacific time, in ISO format?

Time:04-28

I need to work a date object thru a specific set of circumstances.

  1. user is inputting a daterange, from/to.
  2. each date needs to be converted toISOString() because that's how it is in the DB.
  3. client wants the dateranges to hard convert to US Pacific time.

So when the user chooses from/to dates, the app is makes their chosen dates in US Pacific timezone, and then searches the db using ISO formatted dates.

I have this SE Post which gets me the timezone conversion, but in a non-iso format. Any attempts at deriving toISOString() molests the object right back to the device timezone.

I'm trying dayjs(), but it winds up being a bunch of extra steps, only to have the end result be molested to the device timezone.

How can I get the above series of events to unfold in the necessary order?

CodePudding user response:

Here is how you do it with dayjs

const dayjs = require("dayjs");
const utc = require("dayjs/plugin/utc");
const timezone = require("dayjs/plugin/timezone");

dayjs.extend(utc);
dayjs.extend(timezone);
let t = dayjs()
  .tz("America/Los_Angeles")
  .utc(true)
  .toISOString();

console.log(t);

Here is a list of all timezones https://en.wikipedia.org/wiki/List_of_tz_database_time_zones

I personally live in europe, but when i execute this script it shows me the time in los angeles in ISO format

  • Related