How to display timezone like PT, ET instead of PST, EST using moment.js

Viewed 33

I need to use PT/ET instead of PST/EST in order to save the space in table using moment.js

HTML-

{{ moment.tz([2012,0],timezone).format('z')}} // It displaying EST/PST but I need only ET/PT.

Note- Timezone is dynamic, So I want for any timezone it should not show S/D stands for standard/Daylight at position 1.

1 Answers

I suspect the best approach here is to simply replace instances of 'PDT', 'PST' with 'PT' for example. Moment doesn't support creating this abbreviation directly.

const timeZones = ['America/Los_Angeles', 'America/Denver', 'America/Chicago', 'America/New_York', 'America/Halifax']; 

function getAbbr(timeZone) {
    return moment.tz([2022, 1, 1], timeZone).format('z').replace(/[DS]T$/, 'T');
} 

console.log('Timezone'.padEnd(20, ' '), 'Abbreviation')
timeZones.forEach(timeZone => {   
    console.log(timeZone.padEnd(20, ' '), getAbbr(timeZone))
})
.as-console-wrapper { max-height: 100% !important; }
<script src="https://momentjs.com/downloads/moment.js"></script>
<script src="https://momentjs.com/downloads/moment-timezone-with-data.js"></script>

Related