【问题标题】:Invalid argument 'date format' for pipe 'DatePipe'?管道“DatePipe”的参数“日期格式”无效?
【发布时间】:2016-06-09 05:30:10
【问题描述】:

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

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

    ]}
   ]

这是我正在使用的代码 sn-p。

<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 date-format ionic2 pipes-filters


    【解决方案1】:

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

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

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

    工作 plnkr 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

    【讨论】:

    • 谢谢 Pardeep,但作为输出,我得到了 2015-05-18T02:30:56 的 1970 年 1 月 1 日 05:30。如何将 '2015-05-18T02:30:56' 转换为 2015-05-18T02:30:56?
    • 您必须使用新的 Date () 方法对其进行转换,并将所需的日期作为参数传递。看看我的 plnkr,我已经按照你说的更新了代码。
    • newDate = new Date('2015-05-18T02:30:56'); 像这样。
    • 我是 Angular 2 的新手,问题是我从类中传递数据,如何在 html 指令中转换它?例如: .how 我如何隐藏约会。DateSent 到 newDate?
    • 谢谢,这种 set-get 方法非常适合 Chrome 和 Firefox(85.0.2(64 位)中的 Angular 6 应用程序)
    猜你喜欢
    • 1970-01-01
    • 2017-09-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-02-22
    • 2021-12-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多