Angular2 date pipe does not work in IE 11 and edge 13/14

前端 未结 8 1309
闹比i
闹比i 2020-12-05 09:50

I\'m using Angular 2.0 final, and I have an incorrect format of dates when I add hours and minutes in the format string:

In the template of the component, I have:

相关标签:
8条回答
  • 2020-12-05 09:50

    No need to write format like this {{date:dd/MM/yyyy hh:mm:ss a}}.
    IE11 or EDGE getting issue with this format only.

    Here are some alternatives:

    Ex 1: new date and time:

    var date = new Date().toLocaleDateString();
    var time = new Date().toLocaleTimeString();
    
    var datetime = date + " " + time;
    

    Ex 2: Already you have a datetime and you want to convert. see below:

    datetime = "3/23/2018 11:43:51 AM"
    

    We can split the date and time.

    var date = new Date(datetime).toLocaleDateString();
    var time = new Date(datetime).toLocaleTimeString();
    
    var splitdatetime = date + " " + time;
    

    This is Angular4. it work fine for all browsers.

    0 讨论(0)
  • 2020-12-05 09:52

    moment.js pipes for Angular

    Marks answer is great and it was what I was using till I found this simple solution that is well documented.

    moment.js pipes for Angular

    This module works with Angular 2.0 and above.

    0 讨论(0)
  • 2020-12-05 09:54

    Work in Angular 5.x

    In version 5.x of the Angular this problem is working correctly in Edge (v.38.14393.1066.0) and in IE (v.11.1198.14393.0), test in your version!

    Live example: Plunker Angular 5.x


    Related issue that solved the problem (if I found something inconsistent): https://github.com/angular/angular/issues/9524


    Answer that involves the entire operation of the Angular DatePipe.

    0 讨论(0)
  • 2020-12-05 10:00

    UPDATE - The Angular issue that causes this issue is resolved in Angular 5. If you can, I would recommend using that to avoid this problem.

    If you are still using Angular 4 or older - as a workaround, I created a pipe to use the moment formatter instead of the Angular built-in one:

    import { Pipe, PipeTransform } from '@angular/core';
    import * as moment from 'moment';
    
    @Pipe({
        name: 'datex'
    })
    
    export class DatexPipe implements PipeTransform {
        transform(value: any, format: string = ""): string {
            // Try and parse the passed value.
            var momentDate = moment(value);
    
            // If moment didn't understand the value, return it unformatted.
            if (!momentDate.isValid()) return value;
    
            // Otherwise, return the date formatted as requested.
            return momentDate.format(format);
        }
    }
    

    Which can then be used:

    {{exampleDate | datex:'DD/MM/YYYY HH:mm:ss'}}
    

    The date you pass in should be something which moment can parse (see the relevant moment documentation) and the format string is a moment, not angular, date formatting string, as documented here.

    I've tested this in IE11, Chrome and Firefox and it behaves consistently.

    You'll need to ensure moment is added to your package.json as a dependency, e.g.:

    {
      "name": "demo",
      "version": "0.0.1",
      // snip
      "dependencies": {
        // snip
        "moment": "^2.15.1",
        // snip
      },
      "devDependencies": {
        //snip
      }
    }
    

    ... and ensure your systemjs.config.js is updated so it can locate moment:

    map: { 
      'moment': 'npm:moment' 
    } 
    packages: { 
      moment: { main: './moment.js', defaultExtension: 'js' } 
    }
    
    0 讨论(0)
  • 2020-12-05 10:00

    From Angular2 DatePipe API Documentation.

    "this pipe uses the Internationalization API. Therefore it is only reliable in Chrome and Opera browsers.

    0 讨论(0)
  • 2020-12-05 10:02

    If you are okay with showing AM/PM instead of 24 hour time, another valid workaround is to break the formatting into two, and use shortTime or mediumTime for displaying the time portion:

    {{dto.LastExecution | date:'yyyy-MM-dd'}} {{dto.LastExecution | date:'shortTime'}}

    This should work in all major browsers, including IE and Edge.

    0 讨论(0)
提交回复
热议问题