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

Ant*_*ère 42 format datetime pipe internet-explorer-11 angular

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

在组件的模板中,我有:

<th id="lastexecution">{{dto.LastExecution | date:'yyyy-MM-dd HH:mm:ss'}}</th>
Run Code Online (Sandbox Code Playgroud)

IE 11中的输出日期是:

2016-09-27 15:00:9/27/2016 3:53:46 PM:9/27/2016 3:53:46 PM
Run Code Online (Sandbox Code Playgroud)

使用{{dto.LastExecution | 日期: 'YYYY-MM-DD'}}

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

2016-09-27
Run Code Online (Sandbox Code Playgroud)

这是我在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": {}
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*hes 62

更新 - 导致此问题Angular问题在Angular 5中得到解决.如果可以,我建议使用它来避免此问题.

如果您仍在使用Angular 4或更早版本 - 作为一种解决方法,我创建了一个管道来使用矩形格式化程序而不是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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后可以使用:

{{exampleDate | datex:'DD/MM/YYYY HH:mm:ss'}}
Run Code Online (Sandbox Code Playgroud)

您传入的日期应该是片刻可以解析的内容(请参阅相关时刻文档),格式字符串是片刻,而不是角度,日期格式化字符串,如此处所述.

我已经在IE11,Chrome和Firefox中对它进行了测试,它表现得很稳定.

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

{
  "name": "demo",
  "version": "0.0.1",
  // snip
  "dependencies": {
    // snip
    "moment": "^2.15.1",
    // snip
  },
  "devDependencies": {
    //snip
  }
}
Run Code Online (Sandbox Code Playgroud)

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

map: { 
  'moment': 'npm:moment' 
} 
packages: { 
  moment: { main: './moment.js', defaultExtension: 'js' } 
}
Run Code Online (Sandbox Code Playgroud)

  • 我所做的就是重写我的格式以使用这个管道和momentjs.现在工作得很好.+1! (2认同)

小智 15

来自Angular2 DatePipe API文档.

"这个管道使用国际化API.因此它只在Chrome和Opera浏览器中可靠.

  • 他们疯了吗? (33认同)

And*_*ose 10

如果你可以显示上午/下午而不是24小时时间,另一个有效的解决方法是将格式化为两个,并使用shortTimemediumTime显示时间部分:

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

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


小智 5

关于@ mark-hughes上面的答案,从API文档的那一刻开始:

date_expression | 日期格式]

expression是日期对象或数字(自UTC纪元以来的毫秒数)或ISO字符串

参考

因此值应该是任何类型,您可以使用它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; 
    }
}
Run Code Online (Sandbox Code Playgroud)