Angular4无效的管道参数

问题描述 投票:0回答:1

我正在尝试将格式为'2017-12-13T03:00:00Z [UTC]'的日期转换为格式'yyyy / MM / dd'。我得到了这段代码:

this.datepipe.transform(d.hour, 'yyyy-MM-dd HH:mm a z')
this.datepipe.transform(d.initDate, 'yyyy/MM/dd')
this.datepipe.transform(d.endDate, 'yyyy/MM/dd')

在浏览器中我收到此错误:

ERROR Error: InvalidPipeArgument: '2017-12-13T03:00:00Z[UTC]' for pipe 'DatePipe'
at invalidPipeArgumentError (common.es5.js:2610)
at DatePipe.webpackJsonp.../../../common/@angular/common.es5.js.DatePipe.transform (common.es5.js:3506)
at discipinscripcion.component.ts:31
at Array.forEach (<anonymous>)
at DiscipinscripcionComponent.webpackJsonp.../../../../../src/app/components/discipinscripcion/discipinscripcion.component.ts.DiscipinscripcionComponent.inscribir (discipinscripcion.component.ts:29)
at Object.eval [as handleEvent] (DiscipinscripcionComponent.html:21)
at handleEvent (core.es5.js:11998)
at callWithDebugContext (core.es5.js:13467)
at Object.debugHandleEvent [as handleEvent] (core.es5.js:13055)
at dispatchEvent (core.es5.js:8614)

有关将这种日期格式转换为yyyy / MM / dd的任何提示?非常感谢!

angular date formatting date-parsing
1个回答
1
投票

你可以试试这个:

datepipe = new DatePipe(en-US);

或任何其他语言环境。然后将其作为字符串返回

this.datepipe.transform(new Date(), 'yyyy/MM/dd');

不要忘记从DatePipe导入@angular/common并在你的模块中提供providers数组(据说是app.module.ts

© www.soinside.com 2019 - 2024. All rights reserved.