【问题标题】:How to convert date time in to required date format in angular date pipe?如何在角度日期管道中将日期时间转换为所需的日期格式?
【发布时间】: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


【解决方案1】:

角度日期管道适用于日期对象而不是字符串;

myDate = new Date();

然后你的管道就可以正常工作了;

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多