Angular - Displaying Date in Proper Format

Looking for a solution to format a date object from a Java backend using Angular or JS without any plugins? Here's my dilemma: the Java backend only accepts a string and later parses it into a date object.

In my current Angular code, I'm trying:

JSON.parse(oldTime);

To convert the object to a string.

The current output is:

2015-08-12T05:00:00.000Z

But what I need is:

2015-08-12 02:00:00

Answer №1

When displaying a date value in your view, you can utilize the date filter.

Ensure that your HTML code includes both the variable and the filter:

<div ng-app="app" ng-controller="MyCtrl">
    <p>{{time | date:'yyyy-MM-dd HH:mm:ss'}}</p>
</div>

In your JavaScript file, create a date object from a string literal to format the date according to the browser's local time:

var app = angular.module('app', []);
app.controller('MyCtrl', function ($scope, $filter) {
    var jsonvalue = '2015-08-12T05:00:00.000Z';
    $scope.time = new Date(jsonvalue);
    $scope.parsed = $filter('date')($scope.time, "yyyy-MM-dd HH:mm:ss");
});

You can also use the filter module within your controller to parse dates. In the provided example, the parsed variable will store the correctly formatted date as a string.

For a working demonstration, check out this fiddle: http://jsfiddle.net/9pd7rbur/2/

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

Displaying a loading image when opening an external link using window.open

When a pop-up is opened through a window.open() function for external sites, I am looking to display a loading image icon for the links. One approach I considered is to show the loading image in a 'div' and retrieve the content of the external s ...

"Unexpected discrepancy: Bootstrap Glyphicon fails to appear on webpage, however, is visible

I am having some trouble getting the glyphicon to display properly in my side nav. The arrow head should rotate down, which is a pretty standard feature. Here is the link to the page: The glyphicon should be visible on the "Nicky's Folders" top leve ...

Integrating Dynamics CRM with an External Source to Trigger Workflows

Scenario: Imagine a scenario where you want to trigger an existing workflow or a custom action from a webpage located outside the CRM Dynamics environment, such as MS CRM 2011-2013-2015-2016 and 365. Potential Solution: One possible solution could be to ...

Issues with rendering in-line styles in ReactJS after a state update

I'm currently working on implementing a basic state change for a button in React. 'use strict'; class ReactButton extends React.Component { constructor(props) { super(props); this.state = {hovering: false}; } onClick() { ...

What is the process for extracting data from latitude and longitude in order to generate a marker on Google Maps using a script

I have an HTML input text and want to pass coordinates to create a marker on Google maps. Check out the code here: jsfiddle.net/#&togetherjs=r3M9Kp7ff7 What is the best way to transfer this data from HTML to JavaScript? <label for="latitude">L ...

Unable to showcase Angular model in an alternative view

I'm currently working on a sample application where I need to display my model in one view and allow editing in another. Here's the controller code I'm using: var app = angular.module('plunker', ['ngRoute']); app.contro ...

Encountered a Next-Auth Error: Unable to fetch data, TypeError: fetch failed within

I've been struggling with a unique issue that I haven't found a solution for in any other forum. My Configuration NextJS: v13.0.3 NextAuth: v4.16.4 npm: v8.19.2 node: v18.12.1 When the Issue Arises This particular error only occurs in the pr ...

Include a label within the Highcharts Gantt chart square

I'm struggling to include a label within each box in the Gantt Diagram Highcharts, and I'm unsure how to proceed. How can I display a value within each frame? The label needs to be positioned at the center of each box, and all the data is genera ...

Leveraging Angular for making multiple HTTP requests in a loop with a time

I am working with an array of IDs that contains over 50 ids. var ids = [ '3407197', '0632706', '18275', ..., ... ] My goal is to send Angular HTTP GET requests using a loop, with a 10-second delay between ...

Is it better to execute a function before using useState in React?

I am currently developing a random word generator using Next.js. The state of the three randomly generated words is being managed with the help of useState. However, I encountered an error message from React when I tried to implement it in the following ma ...

Discovering the specific style within an inspect-element and transferring it to a JavaScript file

As a novice in the realm of react/javascript, I encountered a situation where I successfully edited a specific style using the inspect element tool. However, despite my efforts, I struggled to locate the corresponding style within the Javascript file. Ha ...

Directive for creating a custom loading indicator in Angular

I have created a custom Angular element directive that displays and hides a loading indicator based on a condition from a service call. The directive is used as an element within another element. While the directive itself works correctly, the issue is tha ...

Transforming ajax code into node.js

After mastering ajax calls for client-server interaction, I am facing a challenge in converting my code to a server-side node compatible JS using http requests. Despite reading various tutorials, I am struggling to adapt them to fit with my current code st ...

The JSON array data is coming back as undefined

Currently facing an issue with the data sa var data = '[{"idcoupons_tbl":"1","discount_percent":"10"}]'; Every time I attempt to parse and retrieve a discount_percent, ie var result= jQuery.parseJSON(data); alert(result["discount_percent"]); ...

Stepping up Your Next.js Game with the Razorpay Payment Button Integration

When using the Razorpay payment button on a website, it provides a code snippet like this: <form> <script src = "https://cdn.razorpay.com/static/widget/payment-button.js" data-payment_button_id = "pl_FNmjTJSGXBYIfp" data ...

The face textures are not being applied correctly to the model imported in THREE.js

I'm having trouble importing a model exported from blender using the THREEJS exporter. The model loads correctly in my scene with the materials applied, such as the car appearing yellow and the glass being transparent as intended. However, I am facin ...

A bar is seen above the directive within the partial

My goal is to design a user interface using Angular on the front end. I have a directive called navbar. Despite using ngRoute, I incorporated this directive in my partial for /home. Everything seems to be functioning properly, but the navbar doesn't a ...

Tips for displaying data by using the append() function when the page is scrolled to the bottom

How can I use the append() function to display data when scrolling to the bottom of the page? Initially, when you load the page index.php, it will display 88888 and more br tags When you scroll to the bottom of the page, I want to show 88888 and more br ...

Expanding form fields using JQuery submit()

I've been attempting to enhance a form by adding additional form fields using JQuery before submitting it. Despite my efforts, I have not been successful with the following Stack Overflow threads: How to add additional fields to form before submit? ...

AngularJS is failing to properly register custom directives

Looking to enhance the SEO caching capabilities of my AngularJS website, I've embarked on implementing a couple of directives for custom page titles and descriptions. Incorporated within my app config (angular.module('website', [...'we ...