管道 'DatePipe' 的参数 'date format' 无效?

Invalid argument 'date format' for pipe 'DatePipe'?

这似乎是一个简单的问题。我在 Ionic 2 应用程序中使用管道来处理日期格式。这是收到的网络服务响应。

 [
  {
    "MessageID": 544882,
    "CategoryID": 1,
    "DateSent": "2015-05-18T02:30:56",
    "Title": "Jobseeker App",
    "MessageContent": "Hi Test guy just started to use the app..",
    "Sender": null,
    "Recipient": null,
    "DateReceived": null,
    "DateRead": "2015-05-18T02:30:56",
    "Note_Direction": "sent",
    "Viewed": 0,
    "AppointmentDateTime": null,
    "MessageAttachments": [

    ]
  },
  {
    "MessageID": 544886,
    "CategoryID": 1,
    "DateSent": "2015-05-18T02:42:45",
    "Title": "Jobseeker App",
    "MessageContent": "App",
    "Sender": null,
    "Recipient": null,
    "DateReceived": null,
    "DateRead": "2015-05-18T02:42:45",
    "Note_Direction": "sent",
    "Viewed": 0,
    "AppointmentDateTime": null,
    "MessageAttachments": [

    ]}
   ]

这是我正在使用的代码片段。

<div class="Date">
<label class="time">{{appointment.DateSent | date:"HH"}}:{{appointment.DateSent| date:"mm"}}</label>
<label class="month">{{appointment.DateSent| date:"MMM"}}</label>
<label class="day">{{appointment.DateSent| date:"dd"}}</label>
<label class="year">{{appointment.DateSent| date:"yyyy"}}</label>
</div>

错误:

Invalid argument '2015-05-18T02:30:56' for pipe 'DatePipe' in [{{appointment.DateSent | date:"HH"}}:{{appointment.DateSent| date:"mm"}} in AppointmentsPage@14:37]

我需要这样的日期格式:

06:05
Dec
24
2015

您传递了错误的参数,因此 angular 抛出错误。用这个更改了你的日期代码:

birthday = 2015-05-18T02:30:56 //Working
birthday2 = new Date(2015-05-18T02:30:56) //Working

Oldbirthday = '2015-05-18T02:30:56'  //Not Working

这里我使用变量 birthday 而不是你的变量名。 也许错误的原因是 angular 可能不接受日期格式为字符串。据我说。但作为官员

  • this pipe is marked as pure hence it will not be re-evaluated when the input is mutated. Instead, users should treat the date as an immutable object and change the reference when the pipe needs to re-run (this is to avoid reformatting the date on every change detection run which would be an expensive operation). https://angular.io/docs/ts/latest/api/common/DatePipe-class.html

工作计划 http://plnkr.co/edit/b9Z090rQpozMoMi0BWaY?p=preview

更新:

根据@Kanishka 的需要是的,您可以更新日期并从 HTML 端转换为 new date() 您必须调用 typescript 的 setter 和 getter 函数对于相同的。这是您正在寻找的示例。所以通过使用它,我认为您不必根据响应创建自己的数组。我希望它可以帮助你,并建议你一种新的方法来玩前端的响应。

<label>{{setDate('2015-05-18T02:30:56') | date:"yyyy"}}</label>

  get Anotherdate(){  //getter function
    return this.date
  }
  setDate(date) {
    this.Anotherdate = date;
    return this.Anotherdate;
  }
  set Anotherdate(date){     // Setter Function
    this.abc = new Date(date)
  }

这里是更新的工作演示http://plnkr.co/edit/rHCjevNcol12vwW6B38u?p=preview