管道“DatePipe"的参数“日期格式"无效? [英] Invalid argument 'date format' for pipe 'DatePipe'?

查看:31
本文介绍了管道“DatePipe"的参数“日期格式"无效?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这似乎是一个简单的问题.我在 Ionic 2 应用程序中使用管道来获取日期格式.这是收到的 Web 服务响应.

This seems to be a simple question. I'm using pipes in my Ionic 2 application for dates format. This is the recieved webservice reponse.

 [
  {
    "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": [

    ]}
   ]

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

This is the code snippet I'm using.

<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]

我需要这样的日期格式:

I need to get a date format like this:

06:05
Dec
24
2015

推荐答案

你传递了错误的参数,所以角度抛出错误.用这个改变了你的日期代码:

You are passing wrong parameters so angular throwing error. changed your date code with this:

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 而不是你的变量名.也许错误的原因是角度可能不接受日期格式作为字符串.据我说.但作为官员

Here I am using variable birthday instead of your variable name. Maybe the reason for the error is angular may not accept the format of date as a string. according to me. But as officials

  • 此管道被标记为纯管道,因此当输入发生变异时不会重新评估它.相反,用户应该将日期视为不可变对象,并在管道需要重新运行时更改引用(这是为了避免在每次更改检测运行时重新格式化日期,这将是一项昂贵的操作).https://angular.io/docs/ts/latest/api/common/DatePipe-class.html

工作 plnkrhttp://plnkr.co/edit/b9Z090rQpozMoMi0BWaY?p=preview

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

As Needed by @Kanishka yes you can update your date and transform into new date() from HTML side you have to call the setter and getter function of typescript for the same. here is an example of what you are looking for. so by using this, I don't think you have to need to create your own array from the response. I hope it may help you and suggest you one new method to play with the response from the front End.

<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=预览

here is Updated working demo http://plnkr.co/edit/rHCjevNcol12vwW6B38u?p=preview

这篇关于管道“DatePipe"的参数“日期格式"无效?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆