【发布时间】:2020-06-18 11:06:56
【问题描述】:
我正在使用 Angular 8。我想将此日期时间格式(“17/06/2020 10:01:09”)转换为这种格式(“17/06/2020”)。但我无法解决这个问题。我收到此错误“InvalidPipeArgument: 'Unable to convert "17/06/2020 10:01:09" into a date' for pipe 'DatePipe'”。
//html
<p>My date: {{myDate | date:'dd/MM/yyyy'}}</p>
//ts 文件
import { Component } from '@angular/core';
@Component({
selector: 'app-root',
templateUrl: './app.component.html'
})
export class AppComponent {
myDate = '17/06/2020 10:01:09';
}
请帮助我找到解决方案。谢谢。
【问题讨论】:
-
你不能只使用 substr(0,10) 吗?
-
您的日期字符串不受支持/不是有效日期。您可以使用 moment 或使用与 new date() 兼容的字符串
-
您的日期字符串格式无效,最快的解决方法是切换月份与日期:
06/17/2020...(MM/DD/YY 是标准格式,不是 DD/MM/YY)
标签: angular datetime date-pipe