AngularJs has the ability to parse dates effectively

So, I have this date: /Date(1451602800000)/.

My goal is to display it in the 'short' format.

Even after attempting {{myDate | date:'short'}}, the output remains as /Date(1451602800000)/

Answer №1

Here's a different method to achieve the same result.
I implemented the regular expression given by @sachila ranawaka
in JavaScript:

.filter('regexp', function(){
    return function(input){
        return input.match(/\/Date\(([0-9]*)\)\//)[1];
    };
});

In HTML:

{{ myDate | regexp | date:'short' }}

This allows you to apply the Angular date filter based on your preference.

Reference: https://docs.angularjs.org/api/ng/filter/date

Answer №2

Implement a custom filter as shown below:

angular.module("myApp",[])
.controller("myCtrl",function($scope){
$scope.date = "\/Date(1451602800000)\/";
})
.filter("customDateFilter", function() {
    var regExp = /\/Date\(([0-9]*)\)\//;
    return function(input) {
        var match = input.match(regExp);
        if(match) return new Date(parseInt(match[1]));
        else return null;
    };
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="myApp" ng-controller="myCtrl">
  {{date | customDateFilter}} <br>
  {{date | customDateFilter | date: 'yyyy-MM-dd HH:mm' }}
</div>

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

Array buffers are scheduled by BinaryJs and the Audio API

My node.js server is streaming some ArrayBuffers: var BinaryServer = require('binaryjs').BinaryServer; var fs = require('fs'); var server = BinaryServer({port: 2000}); server.on('connection', function(client){ var file = f ...

Assigning a distinct value from a database table to the ng-model of a checkbox

In my table, the data is populated with information received from the backend using ng-repeat. Each row has a checkbox, and if multiple checkboxes are selected, I want to send a request to the server to delete those rows. I am trying to bind the checkbox t ...

Tips for saving an array from a form into mongodb

I've created a form structured like this: <form> <label class="label">Class Name</label> <label class="input"> <input type="text" name="class_name[]"> </label& ...

Modify only the visual appearance of a specific element that incorporates the imported style?

In one of my defined less files, I have the following code: //style.less .ant-modal-close-x { visibility: collapse; } Within a component class, I am using this less file like this: //testclass.tsx import './style.less'; class ReactComp{ re ...

React Alert: Unable to update state in an unmounted React component

I have encountered an issue while using Redux in this particular file. When I click on the second component, I receive the following error message: Warning: Can't perform a React state update on an unmounted component. This is a no-op, but it ind ...

What is the method to implement foreach within a Vue select component?

Is there a way to utilize a Vue loop within a select box in order to achieve the following category and subcategory options layout: +news -sport -international +blog In PHP, I can accomplish this like so: @foreach($categories as $cat ...

Is there a way for my JavaScript file to manipulate an EJS variable?

In the index.ejs file, I declared an ejs variable named "complete" which verifies whether a user has completed a journal entry by using the following code: <% if (complete == false) { %> <button class="new_journal">Fill Out Journal<i cla ...

Struggling to render multiple images using Gatsby + GraphQL with `{data.allFile.edges.map(({ node })}`

Description I'm attempting to utilize {data.allFile.edges.map(({ node }) to display multiple local images on a page, but I'm encountering errors when trying to run gatsby develop. Steps to replicate Below is the code I am using: import React fr ...

Implementing Rule for Password Matching in Bootstrap 4 Form Validation

I am utilizing Bootstrap 4 / JS form validation (https://getbootstrap.com/docs/4.0/components/forms/#custom-styles) and it is functioning as expected. Currently, I am working on implementing the same style and submission rules for comparing two password f ...

Using Vuex's v-model to bind to a state field in an object

In my current Vuex state, I have defined a getter named configs, which looks like this: configs: { 1303401: { exampleValue: 'test' } } There is also an input where I bind the exampleValue from the Vuex store's state using v-mo ...

Finding a date after a specific time period using PHP

I am currently utilizing the Yii framework and I have a query regarding getting the current date in PHP (Yii framework) using the following code snippet: $date = new CDbExpression('NOW()'); When executed, it returns the date in the format of "2 ...

`Is there a way to modify the attribute text of a JSON in jQuery?`

I'm attempting to modify the property name / attribute name of my JSON object. I attempted it like this but nothing seems to change. After reviewing the input JSON, I need to convert it to look like the output JSON below. function adjustData(data){ ...

What is the best way to calculate the combined total of radio button values using jQuery or JavaScript?

I recently came across a tutorial on how to sum radio button values using JavaScript or jQuery, and I decided to implement it in my code. However, I encountered some issues as it didn't work as expected. My goal was to offer users the option to downlo ...

Failed deployment of a Node.js and Express app with TypeScript on Vercel due to errors

I'm having trouble deploying a Nodejs, Express.js with Typescript app on Vercel. Every time I try, I get an error message saying "404: NOT_FOUND". My index.ts file is located inside my src folder. Can anyone guide me on the correct way to deploy this? ...

The utilization of the .find method does not yield the desired object when parsing JSON data

Currently, I am utilizing Vue.js 3 for my project, however, I do not believe this is the root cause of the issue I am facing. The problem lies in my attempt to access a JSON array of post objects stored in localStorage. After parsing the array and extracti ...

Tips for restructuring website links on IIS without using the pound sign in combination with AngularJS

By utilizing a URL rewrite in my web.config file, I have enabled the ability to insert URLs that do not contain a # symbol. This allows me to access URLs like , using "name" as a parameter to fetch a template file. Whenever this parameter is used with an X ...

Managing simultaneous tasks with multiple event handlers for a single event

Within the realm of HTML, you may encounter an <input type="file"> element that can be linked to one or more event handlers. However, if one event handler includes asynchronous code, it will pause at that point and move on to the next event ...

Display a loading spinner by utilizing a helper function with the React context API

I'm still learning about the React Context API and I've run into an issue. I'm trying to implement a loader that starts when I make an API call and stops once the call is complete. However, when I try to dispatch actions from a helper functi ...

Retrieve a subsection of an object array based on a specified condition

How can I extract names from this dataset where the status is marked as completed? I have retrieved this information from an API and I need to store the names in a state specifically for those with a completed status. "data": [ { "st ...

Apollo Server Studio is failing to detect the schema configured in Apollo setup

Here is the stack I'm working with: Apollo Server, graphql, prisma, nextjs I've set up a resolver.ts and schema.ts for my graphql configuration under /graphql resolver.ts export const resolvers = { Query: { books: () => books, } ...