2

Object:

const DateTime = luxon.DateTime;
userInfo = {
   startDate: '14/03/2022',
   startTime: '12:30',
   offsetShort: 'EDT',
 }

From the luxon docs I understood to get offset value, Have to use this method:

DateTime.fromObject(new Date(userInfo.startDate), {zone: 'America/New_York'}).toFormat('ZZ');

Will return the offset value like this: -05:00

Since, My api is not returning zone name(ex: America/New_York), I want to get offset value based the offset short name(i,e IST, EST,EDT). Any possibilities in luxon ??

Basically I need any of the below result based on offset short name(EDT/EST/IST):

DateTime.fromObject(new Date('14-03-2022'), {zone: 'EDT'}).toFormat('ZZ'); >> -05:00

 **OR**

DateTime.fromObject(new Date('14-03-2022'), {zone: 'EDT'}).toFormat('ZZ'); >> America/New_York
Shankar
  • 2,565
  • 8
  • 29
  • 56
  • 3
    No, I think that it's not possible to do it in luxon and the use of offset short names is generally discouraged since are ambiguous. See [Time zones and offsets#Terminology](https://moment.github.io/luxon/#/zones?id=terminology) that states: _A named offset is a time zone-specific name for an offset, such as Eastern Daylight Time. [...] They are also ambiguous (...), unstandardized, and internationalized (what would a Frenchman call the US's EST?). For all these reasons, you should avoid them when specifying times programmatically. Luxon only supports their use in formatting_ – VincenzoC Feb 14 '22 at 08:30
  • 2
    `new Date('14-03-2022')` will almost certainly produce an invalid date, see [*Why does Date.parse give incorrect results?*](https://stackoverflow.com/questions/2587345/why-does-date-parse-give-incorrect-results). If you're using Luxon, then use it to parse timestamps also: `DateTime.fromFormat('14-03-2022', 'dd-MM-yyyy',{zone:'America/New_York'})`. – RobG Feb 14 '22 at 08:42
  • 3
    America/New_York infers historic and daylight saving changes that US EDT does not (US EDT didn't even exist until 1966 and its introduction and cessation dates have changed over time). US EDT is -4, not -5 (which is US EST). Most places that observe EDT also observe EST (though Florida is trying to shift to observe EDT all year round). See, it's a mess. :-) – RobG Feb 14 '22 at 08:55

1 Answers1

1

To further illustrate the difficulty here, I've created a findPossibleIanaZones() function. This will take a time zone abbreviation, e.g. EST, EDT, IST and return matching IANA timezone names along with the current offset in minutes.

As you can see, you'll get several matching results, although they will often (but not always!) have the same offset in minutes.

I've used both moment-timezone and luxon to do this, I can't find a luxon-only solution.

I've also created a getOffsetForAbbreviation() function, but it cannot always give us an unambiguous result.

As you can see, IST can refer to Irish Standard Time, Israel Standard Time and of course India Standard Time.

function findPossibleIanaZones(zoneAbbreviation) {
   const timeZones = Object.values(moment.tz._zones).map(z => moment.tz.unpack(z));
   const matchingZones = timeZones.filter(zone => zone.abbrs.includes(zoneAbbreviation));
   return matchingZones.map(z => ({ name: z.name, currentUTCOffsetMinutes: getCurrentOffset(z.name )} )) 
}

function getCurrentOffset(ianaZone) {
    return luxon.IANAZone.create(ianaZone).offset(Date.now());
}

function getUTCOffset(ianaZone, unixTimeMs) {
    return luxon.IANAZone.create(ianaZone).offset(unixTimeMs);
}

function getOffsetForAbbreviation(unixTimeMs, zoneAbbreviation) {
    let zones = findPossibleIanaZones(zoneAbbreviation);
    let offsetNames = zones.map(zone => zone.name);
    let offsets = offsetNames.map(name => getUTCOffset(name, unixTimeMs));
    let uniqueOffsets = [...new Set(offsets)];
    // We should probably throw an error here, null will indicate no value found...
    if (uniqueOffsets.length !== 1) return null;
    return uniqueOffsets[0];
}

const abbrs = ['EDT', 'EST', 'IST'];
for(let abbr of abbrs) { 
    console.log('Timezone abbreviation:', abbr, 'Result:', findPossibleIanaZones(abbr));
    console.log('Current UTC offset:', getOffsetForAbbreviation(Date.now(), abbr));
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

<script src="https://momentjs.com/downloads/moment-timezone-with-data-10-year-range.js"></script>

<script src="https://cdnjs.cloudflare.com/ajax/libs/luxon/2.3.0/luxon.min.js" integrity="sha512-2j5fkjQ4q5ceXgfxi+kqrU2Oz234MrpyywZsQz1F5OGnfat7mOhjRr0oz5cpQ+YwwWB+hhDBSyxNGuL/tKWMFw==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Terry Lennox
  • 29,471
  • 5
  • 28
  • 40