An alternative way to show time zones such as PT and ET instead of using PST and EST with the help of moment

For efficiency in table space using moment.js, I am looking to utilize PT/ET instead of PST/EST.

HTML-

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

Note: The timezone is dynamic, so regardless of the timezone, it should only display ET or PT without S/D indicating standard/daylight time at position 1.

Answer №1

One possible solution is to replace occurrences of 'PDT', 'PST' with 'PT', as Moment does not have built-in support for creating such abbreviations.

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>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

What is the best way to automatically check dynamic checkboxes in Angular reactive forms based on database values?

As a beginner in reactive forms, I am facing challenges with dynamically setting the value of checkboxes to true. For instance, when retrieving pre-selected fruit values for a specific user from the database, I want those fruits to be checked when the page ...

Tips on persisting dynamic form data using JavaScript and a database query

I have a unique script that generates dynamic form content with inputs named "field-1", "field-2", and so on until the last input is created. How can I effectively save this dynamically generated form to the database? Usually, I would create a form with ...

Change the Angular Material 2 theme from light to dark with a simple click

I'm working on an Angular 2 Material project and I want to be able to switch the theme from dark to light with a single click of a button. Can anyone help me figure out how to achieve this? Any tips or advice would be greatly appreciated! ...

Error Occurs While Getting Request Parameters with Ajax Post and Express.js

When utilizing ajax to send data from a JavaScript frontend to an Express.js backend server, I am having trouble retrieving the posted data in the API method of my express.js. Below is the code where I attempt to parse the request data. Your assistance i ...

Transferring callback variables from an Express GET request to a forked process in Node.js

I encountered an issue while trying to transfer the callback variables from app.get() to a forked process. The error message I received was: TypeError: Converting circular structure to JSON The primary goal behind this endeavor is to enable a main node w ...

Vanilla Javascript's alternative to $(document).on

Can someone provide me with the plain vanilla JavaScript code equivalent to the jQuery code below? $(document).on("mousemove touchmove", function(e) { console.log(e.touches[0].pageX); //code }); I understand how to implement this in ...

Tips for showing the database list based on the chosen value in the drop-down menu

manage_bank Hey there, I need some assistance with displaying the bank name based on the selected dropdown option. For instance, if we choose 50 records, it should display 50 records of the bank name from the database. Additionally, I want the selected v ...

Can Node.js Utilize AJAX, and if So, How?

Coming from a background in browser-based JavaScript, I am looking to dive into learning about node.js. From my current understanding, node.js utilizes the V8 engine as its foundation and offers server-side JavaScript capabilities along with pre-installed ...

Mastering the art of utilizing Deferred Lighting in Three.js

I am currently working on a Three.js application that requires multiple independent lights, making it ideal for a deferred lighting renderer. After researching, I found recommendations to use WebGLDeferredRenderer. However, when I tried defining a rendere ...

Feeling unsure about starting the project with React?

I am encountering difficulties while setting up my React project. The errors I am facing are hindering the process, and I am seeking assistance. The new React app is being created at the following location: H:\React Projects\react-js. As I try ...

Using AngularJS to retrieve JSON data with the HTTP module

I am a beginner in the world of angularjs and I need some guidance. Below is the code I have written: <!DOCTYPE HTML> <html ng-app="myapp"> <head> <meta charset="utf-8"> <title>Angularjs project</title> <script type= ...

tracking scroll position within div on main page

I have a div tag enclosed within a content tag due to the implementation of a masterpage containing the forms and body tags. <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server"> <div id="xxx" style="overflow:s ...

Can you come up with a catchy one-liner for an array that contains all the elements of the arrays that came

I am currently working with the array [1,2,3,4,5,6,7,8,9] My goal is to achieve [ [1], [1,2], [1,2,3], [1,2,3,4], [1,2,3,4,5], ... ] I envision the desired outcome as const var = array.reduce **using some form of black magic** I ...

What is the best way to fetch images from a JSON object in a React application?

I have been trying to load images from an array of objects in React, but I keep encountering a "module not found" error. This issue has been frustrating me for the past couple of days. Can someone please help me troubleshoot this problem or provide some su ...

A guide on transferring documents between collections using Mongoose and MongoDB

I have a list of tasks that includes both completed and incomplete items. Additionally, I have two buttons - one for deleting an item (which is functional) and the other for marking an item as done or not. I am struggling with creating a function to move ...

JavaScript - Saving an array of objects to an .xlsx file with various header rows that are merged

I am faced with the challenge of recreating an Excel file that was manually created programmatically. I currently have a node.js(TS) service that exports data into a csv file, but the formatting is not as desired. After some research, I couldn't find ...

What is the method to modify an entity by utilizing its foreign key in Prisma?

My API allows authenticated users to perform CRUD operations on videos. Each video has a creatorId foreign key that references the User model. I encountered an error when trying to update a video by passing the creatorId into the where clause of Prisma fun ...

Learn the steps to successfully rotate the custom icon in Material-Ui select without impacting its functionality and making sure it remains clickable

I've been trying to incorporate my own icon for the material UI select component. I successfully replaced the default icon using the "IconComponent" attribute in MU select. However, I'm facing issues with the new icon not rotating when the menu ...

Sending numerous array values from datatables

I am currently working with datatables that allow for multiple selections. When I select 3 rows from top to bottom (as shown in this picture), the console.log returns this specific value for each row selected. The value that needs to be passed to the next ...

Is there a way to identify the top five elements that are most frequently occurring in the array?

I've successfully found the element that appears the most in an array, but now I'm facing a new challenge where I need to identify the top 5 elements that appear most frequently. Here is the array: [ "fuji", "acros", &q ...