Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
221 views
in Technique[技术] by (71.8m points)

Formatting date inside of component (ionic)

Im working an app with appery.io platform using ionic 1 I have a date value inside of text component.

If I use:

{{user.user_date}} 

Return

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

If I use:

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

Return

22/01/2021 // today date

How can I format this date value inside of component?

Thanks

question from:https://stackoverflow.com/questions/65845174/formatting-date-inside-of-component-ionic

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Your question can be summarize as "How to use a pipe in a Typescript file". Where your can find the solution here Is it possible to use a pipe in the code?

You need to import the pipe amDateFormat in the providers of your module. Then, you need to inject the pipe in the constructor of the component. Finally call the transform function of the pipe.

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

You need to change DatePipe with the name of the pipe you as using. The code works in the last version of Ionic/Angular, not tested in Ionic 1.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...