Angular2 日期管道在 IE 11 和 edge 13/14 中不起作用

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

我使用的是 Angular 2.0 final,当我在格式字符串中添加小时和分钟时,日期格式不正确:

在组件的模板中,我有:

<th id="lastexecution">{{dto.LastExecution | date:'yyyy-MM-dd HH:mm:ss'}}</th>

IE 11 中的输出日期为:

2016-09-27 15:00:9/27/2016 3:53:46 PM:9/27/2016 3:53:46 PM

有 {{dto.LastExecution |日期:'yyyy-MM-dd'}}

IE 11 中的输出日期是正确的:

2016-09-27

这是我在 package.json:

中使用的组件版本
{
  "name": "ima_sentinel",
  "version": "1.0.0",
  "description": "QuickStart package.json from the documentation, supplemented with testing support",
  "scripts": {
    "start": "tsc && concurrently \"tsc -w\" \"lite-server\" ",
    "docker-build": "docker build -t ima_sentinel .",
    "docker": "npm run docker-build && docker run -it --rm -p 3000:3000 -p 3001:3001 ima_sentinel",
    "pree2e": "npm run webdriver:update",
    "e2e": "tsc && concurrently \"http-server -s\" \"protractor protractor.config.js\" --kill-others --success first",
    "lint": "tslint ./app/**/*.ts -t verbose",
    "lite": "lite-server",
    "postinstall": "typings install",
    "test": "tsc && concurrently \"tsc -w\" \"karma start karma.conf.js\"",
    "test-once": "tsc && karma start karma.conf.js --single-run",
    "tsc": "tsc",
    "tsc:w": "tsc -w",
    "typings": "typings",
    "webdriver:update": "webdriver-manager update"
  },
  "keywords": [],
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@angular/common": "2.0.0",
    "@angular/compiler": "2.0.0",
    "@angular/core": "2.0.0",
    "@angular/forms": "2.0.0",
    "@angular/http": "2.0.0",
    "@angular/platform-browser": "2.0.0",
    "@angular/platform-browser-dynamic": "2.0.0",
    "@angular/router": "3.0.0",
    "@angular/upgrade": "2.0.0",
    "angular2-in-memory-web-api": "0.0.20",
    "bootstrap": "^3.3.6",
    "core-js": "^2.4.1",
    "linqts": "^1.6.0",
    "reflect-metadata": "^0.1.3",
    "rxjs": "5.0.0-beta.12",
    "signalr": "^2.2.1",
    "systemjs": "0.19.27",
    "typescript-collections": "^1.1.9",
    "zone.js": "^0.6.23"
  },
  "devDependencies": {
    "concurrently": "^2.2.0",
    "lite-server": "^2.2.0",
    "typescript": "^2.0.2",
    "typings": "^1.0.4",
    "canonical-path": "0.0.2",
    "http-server": "^0.9.0",
    "tslint": "^3.7.4",
    "lodash": "^4.11.1",
    "jasmine-core": "~2.4.1",
    "karma": "^1.2.0",
    "karma-chrome-launcher": "^0.2.3",
    "karma-cli": "^0.1.2",
    "karma-htmlfile-reporter": "^0.2.2",
    "karma-jasmine": "^0.3.8",
    "protractor": "^3.3.0",
    "rimraf": "^2.5.2"
  },
  "repository": {}
}

来自 Angular2 DatePipe API 文档。

"此管道使用国际化 API。因此它仅在 Chrome 和 Opera 浏览器中可靠。

更新 - Angular issue that causes this issue 已在 Angular 5 中解决。如果可以,我建议使用它来避免此问题。

如果您仍在使用 Angular 4 或更早版本 - 作为解决方法,我创建了一个管道来使用 moment 格式化程序而不是 Angular 内置的:

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);
    }
}

然后可以使用:

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

您传入的日期应该是 moment 可以解析的日期(参见 the relevant moment documentation) and the format string is a moment, not angular, date formatting string, as documented here

我已经在 IE11、Chrome 和 Firefox 中对此进行了测试,它的行为始终如一。

您需要确保 moment 作为依赖项添加到您的 package.json 中,例如:

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

... 并确保您的 systemjs.config.js 已更新,以便它可以找到时刻:

map: { 
  'moment': 'npm:moment' 
} 
packages: { 
  moment: { main: './moment.js', defaultExtension: 'js' } 
}

关于上面@mark-hughes 的回答,从 API 文档的那一刻起:

date_expression | date[:format]

expression is a date object or a number (milliseconds since UTC epoch) or an ISO string

Reference

所以这个值应该是任何类型,你可以使用moment().isValid()来检查值类型

@Pipe({name: 'datex'})
export class DatexPipe implements PipeTransform {
    transform(value: any, format: string = ""): string {
       return moment(value).isValid()? moment(value).format(format) : value; 
    }
}

如果您同意显示 AM/PM 而不是 24 小时制,另一个有效的解决方法是将格式分成两部分,并使用 shortTimemediumTime 来显示时间部分:

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

这应该适用于所有主流浏览器,包括 IE 和 Edge。

在Angular工作5.x

在 Angular 的 5.x 版本中,此问题在 Edge (v.38.14393.1066.0) 和 IE (v.11.1198.14393.0) 中正常工作,请在您的版本中测试!

实例:Plunker Angular 5.x


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


moment.js 管道 Angular

Marks 的答案很棒,在我找到这个有据可查的简单解决方案之前,我一直在使用它。

moment.js pipes for Angular

此模块适用于 Angular 2.0 及更高版本。

以下解决方案适用于 IE11 和 chrome。无需创建自定义管道。

 {{dto.createdTimeLocal | date:'dd MMM yyyy'}} {{dto.createdTimeLocal | date:'shortTime'}}

不需要这样写格式{{date:dd/MM/yyyy hh:mm:ss a}}
IE11 或 EDGE 仅对此格式有问题。

这里有一些备选方案:

示例 1:新日期和时间:

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

var datetime = date + " " + time;

示例 2:您已经有 datetime 并且想要转换。见下文:

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

我们可以拆分日期和时间。

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

var splitdatetime = date + " " + time;

这是 Angular4。它适用于所有浏览器。