Is the Angularjs Date formatting consistent across different browsers?

Below is the code I am working with:

app.filter('myDateFormat',function myDateFormat($filter){
    return function(text){
        var tempdate= new Date(text.replace(/-/g,"/"));
        return $filter('date')(tempdate, "dd-MM-yyyy HH:mm:ss");
    }
});

This filter takes the date from a database and formats it correctly. Here is how it is used in the HTML:

<li ng-repeat="rows in latestqs | limitTo:12">
   <a href="#pubres/{{ rows._id }}"> {{ rows.title }} </a>
   <br>
   <small>by {{ rows.group_name }} @ {{ rows._createdAt | myDateFormat }}</small>
 </li>

While this code works smoothly on Chrome, it encounters issues on IE9 where it displays as:

NaN-NaN-NaN-0NaN NaN:NaN:NaN

On Chrome, however, it shows as:

19-03-2014 14:00:19

Are there any suggestions on how to fix this problem?

Answer №1

Incorporate momentjs into your filter for optimal date handling...

app.filter('myDateFormat', function myDateFormat($filter){
    return function(text){
        var moment = moment(text);
        if(moment.isValid) {
          var tempdate= new Date(moment.value());
          return $filter('date')(tempdate, "dd-MM-yyyy HH:mm:ss");
        }
    }
});

This code has not been tested yet, but it is expected to perform well in all browsers...

Answer №2

Encountered a similar issue recently. I had a function structured as follows:

function(text) {
  var d = new Date(text);
  return d;
}

The root cause of the problem was traced back to the Date constructor. More specifically, IE9 struggled with parsing certain date/time strings accurately. It appeared that instances where milliseconds were less than 100 caused issues. Upon closer examination, I observed that when there were fewer than 100 milliseconds, only two digits were displayed instead of three. This missing third digit led to IE9 encountering errors. As a workaround, I modified the code to include an additional zero (or two if milliseconds were in single digits). This adjustment effectively resolved the issue at hand.

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

Discover the rotation direction while dragging - GreenSock Animation Platform

I am currently implementing the Spin feature of the Greensock library for a dial element. While using the getDirection() method, I noticed that it accurately determines if the dial is rotating clockwise or counterclockwise when passing the starting point. ...

I need assistance with an issue on my Google Dev Console, as it keeps showing an error stating that ".getcontext is

Looking for some assistance here as my development console keeps displaying an error: Uncaught TypeError: canvas.getContext is not a function. Here is the problematic code snippet: `var canvas = document.createElement; var c = canvas.getContext("2d&qu ...

The ng-show directive is failing to update properly after changes are made to the scope values

I'm experiencing some issues with the ng-show method. I have set it up like this: Even though the username string length is checked, the ng-show method doesn't seem to hide/show the extra text until after another keystroke. How can I make it upd ...

I am unable to utilize third-party components within my Nuxt.js/vue.js project

I am attempting to use a library for my Nuxt project, following the guidelines laid out in the documentation available here: getting-started Despite following the instructions provided, I keep encountering errors such as "Unknown custom element: - did you ...

Ways to extract tar data from a hexadecimal representation

After making a RESTAPI call to an endpoint, I was expecting to receive .tar contents. However, what I received appears in hexadecimal format as: <Buffer 4d 4d 00 2a 00 00 00 08 00 13 01 00 00 03 00 00 00 01 01...... The function connectForTar demonstra ...

Modular Angular Component

I'm currently developing a language learning application using Angular 15. With numerous pages and components featuring diverse content, I am exploring the option of allowing users to customize the font type, size, and other display settings for each ...

PHP sends a message to JavaScript to pause execution

The title may be a bit unclear, but here is my issue: I have JavaScript making an AJAX Request to the PHP Controller. In the PHP Controller, a Thread is created and initiated with the following code: public function run() { while(true) { //D ...

Following the execution of the Ajax function in JavaScript, the functionality of the

My JavaScript validation function checks if an input value is empty. I now want to enhance it by adding a validation that ensures a special input is not being repeated. I have written a PHP function and utilized AJAX for this purpose. The AJAX part seems t ...

Troubleshooting my code: The mystery of why Google Map directions won't cooperate!

Can anyone help me figure out why my code for drawing a path between multiple points on a map isn't working? I've tried the code below, but it doesn't draw any paths. What could be causing this issue and how can I solve it? var myTrip = [] ...

What is the best way to populate this dynamic form with data retrieved from the server?

I found a helpful Bootsnipp that utilizes BootStrap.JS to generate dynamic fields and collect data from forms. If, after saving the form, I receive JSON data from the server in key-value pairs, how can I build this dynamic form so that users can easily up ...

React Braces Dilemma

I'm a bit embarrassed to admit it, but I'm struggling with writing something simple in React using JSX. Despite searching online and consulting various resources, including the official React documentation, I am still unable to grasp the concept. ...

What steps can I take to avoid receiving an error stating that parameter 1 is not of type 'Node'?

I encountered the following error message: Uncaught TypeError: Failed to execute 'serializeToString' on 'XMLSerializer': parameter 1 is not of type 'Node'. I am unsure why this error is occurring. It works fine on localhost ...

What is the process for invoking an MVC Api controller's Post action with a HttpResponseMessage using JavaScript as the client

Currently, I am making an API call from the client side. Below is the code snippet for both the client and server sides: API Action in C#: public class StudentTestController : ApiController { [HttpPost] public HttpResponseMessage GetL ...

What causes the erratic behavior of zooming on my website when using mobile devices?

My website is experiencing difficulties on mobile devices despite my efforts to enable zooming. I have tried various methods, including experimenting with meta tags like user-scalable=1, user-scalable=true, and adjusting min and max values, but the issue p ...

Repeatedly utilizing a drop-down menu

Can a <select> menu be written and utilized in multiple locations on a webpage? For instance: <select id="dm"> <option value="">Select Quantity</option> <option value="less ">50 - 60</option> <option value="me ...

Modify KeyboardDatePicker to display the full name of the day and month

Date Selector Hey there, I'm looking to modify the date format from Wed, Apr 7 to Wednesday, April 7. Is there a way to display the full name of the day and month instead of the short abbreviation? ...

Ways to reposition JavaScript output within a webpage

I am new to HTML, CSS, and JavaScript and I have a question regarding how they work together. I can use JavaScript to display objects on the page, but I'm unsure how to move them around like other elements. Is CSS the solution for this? Any advice w ...

Deactivate the focus state when hovering over different items in the navigation bar

Currently facing an issue with my Navbar items having underline animation on hover. Once clicked, the animation persists. However, when hovering over a neighboring navbar item, the underlines appear next to each other, creating the illusion of one long lin ...

Changing directions while the script is running

For my web site testing, I created a script using Tampermonkey that randomly browses the site and modifies parameters. Sometimes I need to navigate to a different page by either using window.location.replace(URL) or by clicking a button with the script l ...

What is the best approach to streamlining the calculation of profile completion with numerous if statements?

Currently, I have a functional code for calculating profile completion percentage. Each field in the user profile is given a weight, and if that particular field is filled out, the corresponding weight is added to the completion score. Although this appr ...