Convert the date to the standard JavaScript format

After receiving a JSON response with a date property in the format of yyyy-MM-dd, I am aiming to convert it into the default JavaScript format which is:

Mon Jun 13 2016 16:35:48 GMT-0400 (Eastern Daylight Time)
using Angular.js.

I carefully reviewed the date filter documentation available at https://docs.angularjs.org/api/ng/filter/date. It seems that the "fullDate" format mentioned there includes a timezone, but doesn't match the desired format exactly.

I am wondering if there is a method in Angular to achieve this conversion by utilizing the $filter object?

Answer №1

Transform a string into a date variable:

$scope.date = new Date('2016-06-13'); // or new Date(String(dateStringVariable));

You can now apply a date filter like this:

$scope.format = "EEE MMM dd yyyy hh:mm:ss 'GMT'Z '(Eastern Daylight Time)'";

HTML:

{{date | date: format}} 

See the code in action on jsfiddle

Answer №2

You have the ability to create a personalized filter

angular.module("yourApp").filter('displayJSDate', function(){
  return function(inputString){
    //inputString follows the format mm-dd-yyyy
    var myDate = new Date(inputString);
    //modify the date object as desired
    //if no modifications are made, it will return a string like Mon Jun 13 2016 16:35:48 GMT-0400 (Eastern Daylight Time)
    return String(myDate);//explicitly converting to a string

  }
})

If you are using markdown syntax, simply apply this filter using the '|' symbol

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

Access row information within a function while incorporating a data-table component

I am currently facing a challenge with my custom Data-Table being utilized by all developers in the workspace. Due to its complexity, I find it difficult to make changes for minor tasks and have decided to explore alternative solutions. The Data-Table is ...

Exploring the benefits of utilizing express-session for authentication management

I am currently in the process of creating a basic login application using express 4 and express-session. When setting up my code as follows: app.use(session({ store: new MongoStore({ db: 'sess' }), secret: 'Ninja Turtle', cookie ...

Update the content of a div and refresh it when a key on the keyboard is pressed

I need to hide the images in a div when I press a key on the keyboard. How can I achieve this? <div> <span role="checkbox" aria-checked="true" tabindex="0"> <img src="checked.gif" role="presentation" alt="" /> ...

Encountering difficulty when determining the total cost in the shopping cart

I am currently working on a basic shopping cart application and I am facing an issue when users add multiple quantities of the same product. The total is not being calculated correctly. Here is my current logic: Data structure for Products, product = { ...

Modify JSON date format to a shorter version using the first 2 letters of the month and the year

Looking to convert date values in a JSON array from "December 2016" format to "D16". Hoping to accomplish this using Regex, any assistance would be greatly appreciated. [["November 2016","December 2016","January 2017","February 2017","March 2017"],["tot ...

In AngularJS, if you use $q with the sequence then() followed by catch() and then() again, you can halt the execution of

Here's a concise version of the code that I'm using: var makeRequest = function (method, url, query, data) { var request = { method: method, url: url, params: query || {}, paramSerializer: '$httpParamSeri ...

Detect the initial collision exclusively (Collision detection using raycasting)

I am currently working on a never-ending runner game where a character is positioned at (0,0) and can only move along the X-axis by using arrow keys or the mouse. There are two different types of objects moving from z = -10000 towards the character, and on ...

Arrange and display similar objects together

I have a list of items in a listView that need to be visually grouped based on their class, displayed within boxes. For example, I have 5 items with the following classes: <div class="1"></div> <div class="1"></div> <div class= ...

Issue encountered while trying to access Contentful's API using the getEntry() helper: promise for retrieved entry does not return any content

Introduction A few days ago, I reached out for assistance regarding the Contentful API and a javascript query issue on Stack Overflow. Since then, I have been grappling with understanding how to effectively utilize this API in conjunction with NextJS. Th ...

Connecting a href link to a TAB

Check out this useful CODE example I am using. I have a webpage with links that have href attributes. Now, I want to link these pages to another page and have them automatically open a specific tab when clicked. Is this possible? Here are the links on th ...

Error: Unhandled promise rejection - The method this.~(...) does not support chaining with .then() function

I'm currently working on a project that involves fetching data from a database. I am attempting to call the function getUserById() (which will use axios to communicate with the database later), and I'm encountering an issue with the line this.get ...

Reloading the ASP.NET MVC bootstrap modal using Ajax for a fresh look

I'm struggling with my bootstrap modal. Whenever I click the submit button, the page refreshes and the modal disappears. How can I keep the modal open after clicking the submit button to display either a success or error message? I am new to MVC and h ...

Execute the controller function with the value as a parameter

I encountered an issue while attempting to call a function in the c# controller and passing a value. The error message I received was: `'Unable to get property 'then' of undefined or null reference'. I also included the Driver Model but ...

A guide to modifying the color of the Menu component in material-ui

Currently, I am utilizing the Menu component from material-ui and facing an issue while trying to modify the background color. The problem arises when I attempt to apply a color to the Menu, as it ends up altering the entire page's background once the ...

altering saved user information

In my current setup using Passportjs for user authentication on an angular + sailjs stack, I am saving a user object (containing role, name, email) in an angular controller for other controllers to access. However, as a newbie to the JavaScript stack, I ...

Strategies for re-rendering a React component when the useState value remains the same or retains its previous value

I am currently using a useState hook to store the value of selectFolderId: const [selectFolderId, useSelectFolderId] = React.useState(documentStore.id) Despite trying to update selectFolderId with the new value from DocumentStore by using a useEffect hook ...

Creating multiple objects using a single object in JavaScript can be achieved by using the concept of object

When presented with the following data structure: { "time_1": 20, "time_2": 10, "time_3": 40, "time_4": 30 } and expecting a result in this format: [ { "key1": "time_1" ...

An issue occurred while employing Promise.all: TypeError - the intermediate value is not iterable within the code

I am currently attempting to halt a series of SQL instances, and due to its asynchronous nature, I have implemented the use of Promise.all. However, I'm encountering an error in the code on: TypeError: (intermediate value) is not iterable at Prom ...

Angular bar chart tooltip feature malfunctioning

Hey there! I'm currently using an Angular bar chart and it's working smoothly. However, I'm facing an issue with displaying tooltips as the series is not functioning correctly. Below is my code snippet. My goal is to have a tooltip appear wh ...

Updating the parent page host within a cross-domain iframe: issues encountered in Firefox and Chrome browsers

I am encountering an issue with my iframe app where I am receiving an alert indicating "-error" in Chrome related to top.location.href. jQuery.ajax({ type : 'get', url : 'check_if_fb_data_set.php', success ...