小能豆

How to enforce a specific timezone ignoring system time

javascript

I am trying to convert an ISO date to EST, so that the user always sees EST, regardless of the system.

Like so

luxon.DateTime("2023-04-16T03:00:00Z").setZone("EST").toFormat('ttt')

Yet it always comes out like so:

10:00:00 PM GMT-5

I want:

10:00:00 PM EST

What am I doing wrong? I know EST is IANA compliant.


阅读 74

收藏
2023-12-01

共1个答案

小能豆

It seems like Luxon is displaying the offset and abbreviation instead of just the abbreviation for the time zone. To achieve your desired format, you can use the toFormat function with a custom format string. Here’s an example:

luxon.DateTime.fromISO("2023-04-16T03:00:00Z")
  .setZone("America/New_York") // Use the IANA time zone identifier for EST
  .toFormat('hh:mm:ss a z');

In this example:

  • hh:mm:ss a formats the time as hours, minutes, seconds, and AM/PM.
  • z includes the time zone abbreviation.

This should give you the output you’re looking for:

10:00:00 PM EST

Make sure to replace "America/New_York" with the correct IANA time zone identifier for the Eastern Standard Time (EST) zone. Luxon relies on the IANA time zone database for accurate time zone information.

2023-12-01