Receiving an invalid date parsing value result

Having some trouble parsing a date in Angular with the following code snippet:

$scope.startDate = '2016-12-16 15:11:52'
$start = Date.parse($filter('date')($scope.startDate,'dd-MM-yyyy'));

Unfortunately, the value returned is NaN.

Answer №1

Give this code a try instead

$scope.startDate =  '2016-12-16 15:11:52'  
$start = $filter('date')(new Date($scope.startDate.replace("-","/")),'dd-MM-yyyy');

Explanation

The filter function is meant to format only date objects, but you provided a string. Therefore, we need to convert the string to a date object. The new Date() function does not directly convert the date format with dashes (-), which is why I used the replace method to change dashes (-) to slashes (/). This adjustment should make it work correctly. I have tested this solution on

Check out the demonstration on w3schools

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

Using ng-bind for interpolation with AngularJS translation

When trying to render special characters like < and > in a user's first or last name using angular translate, I encounter an issue where the characters are not displayed. I have attempted using ng-bind="translation_key | translate | values" but ...

The value of the progress bar in Bootstrap Vue cannot be retrieved using a function

I have implemented a progress bar using Bootstrap Vue. Here is the HTML code: <b-progress :value="getOverallScore" :max=5 variant="primary" animated></b-progress> The getOverallScore function calculates an average value based on three differe ...

Challenges encountered with Material-UI elements

Attempting to implement the http://www.material-ui.com/#/components/drawer (Docked Example) component from Material-UI in conjunction with ReactJS. An error is encountered with the "=" sign in this line: handleToggle = () => this.setState({open: !this ...

Troubleshooting Uploadify Issues

UPDATE: I discovered that the problem was related to Uploadify not having a session, which prevented it from accessing the designated page. To avoid this issue, simply direct it to a page without any admin login security ;) The issue stemmed from Upload ...

Verify if the value in a textbox exceeds the number entered in an array of textboxes

My goal is to compare the value of a single text box, labeled as "totalmarkstoall", with multiple arrays of text boxes labeled as "marksscored". The JavaScript code below is set up to compare these values using a key up function. The issue I am facing is ...

The failure of jQuery AJAX error callback to execute

I'm currently facing an issue with an AJAX call that I have in my code. Here's the code snippet: $.ajax({ type: "get", url: "xxx.xxx.xxx/xxx.js", dataType: 'jsonp', success: function(data) { ...

Is there a way to make images load only when the navigation buttons are clicked in a scrollable (jquery tools) rather than loading all images at once?

I came across this great demo tutorial that I'm currently following: The issue with the tutorial is that it loads all the images at once, which significantly impacts performance. My goal is to have it load a set of images each time you click the arro ...

Encountered a build issue following the Svelte update: The subpath package './compiler.js' is not recognized by the "exports" definition

Challenge After updating from Svelte version 3.0.0 to the latest using npm i svelte@latest, I encountered an issue where my application would not run and constantly displayed the following error: [!] Error: Package subpath './compiler.js' is n ...

Discovering the exact latitude and longitude coordinates along a specific route using Google Maps

I've encountered a problem with finding the latitude and longitude along a given route using Google Maps DirectionsService. I have a JSON dataset containing latitude, longitude, and price details. My goal is to plot markers on the map and determine wh ...

Organizing nested files with JAWR: a systematic approach

Recently, I started using JAWR to bundle my AngularJS files. However, I encountered an issue where the files were being loaded out of order. Here is my directory structure: app app.js folderA something.controller.js something.module.js I want the f ...

Detaching the jQuery event where the context is tied to the handler

My current challenge involves removing a jQuery event with a callback function bound using this. The issue arises from the fact that .bind() generates a new function each time it is called, causing difficulties when trying to remove the event. I am strugg ...

The preflight request for OPTIONS is receiving a 400 bad request error on the secure HTTPS

When making an Ajax call on the front end and calling a WCF service through Ajax, I encountered an issue with adding headers. As a result, a preflight OPTIONS request is triggered but fails due to the URL being blocked by CORS policy. I tried adding the f ...

Is it possible to implement Ajax functionality in JavaScript without using XMLhttp and connection the same socket for every request?

Can data on a page be communicated and updated without the need for reloading, all while avoiding the XMLHttpRequest object and using the same connection or socket for each request (without closing the connection each time)? ...

The shadow effects and color overlays do not seem to be functioning properly in Mozilla Firefox

I have designed a popup registration form using the Bootstrap modal class. To ensure form validation, I have integrated some jQuery validation engine functionality. Additionally, I customized the appearance by adding a box shadow, adjusting the background ...

There seems to be an issue with declaring Gulp Open

Here is the code snippet for my `open.js` task: import gulp from 'gulp'; import gulpOpen from 'gulp-open'; gulp.task('open', () => { // doesn't work with `function()` either. const options = { uri: 'local ...

What is the proper way to implement ng-click in a link call within a directive?

http://plnkr.co/edit/Ry8gkozAaudhlmdPPQie?p=preview In the plunkr example provided, I am experimenting with creating a directive that can display different templates based on the presence of attributes on the directive element. <my-dir></my-dir& ...

Creating a virtual roulette wheel with JavaScript

I'm currently working on creating a roulette wheel using JavaScript. While researching, I came across this example: , but I wasn't satisfied with the aesthetics. Considering that my roulette will only have a few options, I was thinking of using ...

Choosing specific content from a different div in JavaScript based on the line number

I have a question regarding two div elements, div1 and div2. I am trying to develop a function where in div2, I can specify the line number of div1 and display the relevant content from that specific line. Currently, I have created a function that successf ...

adjusting the template of the @Component programmatically

The template I am using is generated dynamically based on the intricate rules defined in the Java layer. I am wondering if there is a method to dynamically assign or modify a component's template during ngOnInit or any other suitable point in the cod ...

Make sure to preserve the sorted list before adding an additional filter with ng-repeat

I have a unique list named ListA. After applying various filters, the filtered results are saved in a new array called filteredList. <div ng-repeat="element in filteredList =(ListA| filter: {label:searchCategory.value} |filter:searchString)"> Now, ...