这似乎是一个简单的问题。我在 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
关于angular - 管道 'date format' 的参数 'DatePipe' 无效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35644275/