【问题标题】:Formatting date inside of component (ionic)格式化组件内部的日期(离子)
【发布时间】:2021-01-22 12:26:50
【问题描述】:

我正在使用 ionic 1 在 appery.io 平台上运行应用程序 我在文本组件中有一个日期值。

如果我使用:

{{user.user_date}} 

返回

{"$date":"1958-12-08T00:00:000Z"}  // Correct value but wrong format

如果我使用:

{{user.user_date | amDateFormat:'DD/MM/YYYY'}}

返回

22/01/2021 // today date

如何在组件内部格式化这个日期值?

谢谢

【问题讨论】:

  • format this date value inside of component 是什么意思?你的意思是在component.htmlcomponent.ts 文件内还是不同的东西?
  • Ionic 1 使用 AngularJS?然后阅读:stackoverflow.com/questions/20131553/…
  • 嗨,component.ts
  • 但解决方案可以在 javascript 中,然后我将值传递给组件

标签: date ionic-framework appery.io


【解决方案1】:

您的问题可以概括为“如何在 Typescript 文件中使用管道”。在哪里可以找到解决方案Is it possible to use a pipe in the code?

您需要在模块的providers 中导入管道amDateFormat。然后,您需要在组件的构造函数中注入管道。最后调用管道的transform函数。

constructor(
    private datePipe: DatePipe
    ) {
    return this.datePipe.transform(user.user_date, "DD/MM/YYYY");
}

您需要将DatePipe 更改为您正在使用的管道名称。该代码适用于 Ionic/Angular 的最新版本,未在 Ionic 1 中测试。

【讨论】:

    【解决方案2】:

    您需要观看 appery.io youtube 视频如何使用管道 https://www.youtube.com/watch?v=rzMiIKeaJcc

    【讨论】:

    • 我认为在 StackOverflow 之外提供解决方案并不是一个好主意。
    猜你喜欢
    • 2015-05-27
    • 2019-05-14
    • 2021-11-06
    • 2021-04-14
    • 2013-08-07
    • 1970-01-01
    • 2020-03-20
    • 1970-01-01
    • 2023-03-29
    相关资源
    最近更新 更多