0

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?

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
monsto
  • 1,178
  • 1
  • 13
  • 26
  • [How to convert a date to an ISO format](https://stackoverflow.com/questions/25159330/how-to-convert-an-iso-date-to-the-date-format-yyyy-mm-dd). [How to convert a Date's timezone](https://stackoverflow.com/questions/70019265/i-want-to-change-timezone-of-date-object-in-javascript) – Tibrogargan Apr 27 '22 at 22:16
  • "ISO format" is a way of viewing the underlying point in time. How are you determining the result has been "molested"? Because the default value shown by many JavaScript debuggers is the current device's time zone view of the point in time. – Heretic Monkey Apr 27 '22 at 22:16
  • Does this answer your question? [Convert date to another timezone in JavaScript](https://stackoverflow.com/questions/10087819/convert-date-to-another-timezone-in-javascript) – Heretic Monkey Apr 27 '22 at 22:17
  • Please see [Date](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Date). All JS Dates are UTC, there is no timezone component stored in the Date object, but you can represent them in a locale. (Which is probably what you need to do when translating between your data model and and your data layer) – Tibrogargan Apr 27 '22 at 22:19
  • Compare: `console.log((new Date(0)).toGMTString())` to `console.log(new Date(0))`. These will be identical if your browser says it's in the GMT TIme Zone, but be different anywhere else. Either way, `new Date(0)).toGMTString()` will always be "Thu, 01 Jan 1970 00:00:00 GMT", no matter where you are. – Tibrogargan Apr 27 '22 at 22:33
  • It struct me that a number of databases I've worked with have functions to convert millis since the epoch (i.e. a timestamp) to the system timezone. Native support would beat doing a string conversion in JavaScript. – Tibrogargan Apr 27 '22 at 23:09
  • @HereticMonkey that is the exact post that I linked saying "but in a non-iso format". So no, it did not answer the question when I found it initially. – monsto Apr 28 '22 at 01:16
  • But it answers it now. Because it does the same thing as what your currently accepted answer does, without dayjs. I've added the [dayjs] tag, because apparently answers using that library are acceptable (albeit also duplicates). – Heretic Monkey Apr 28 '22 at 22:25

1 Answers1

1

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

bill.gates
  • 14,145
  • 3
  • 19
  • 47