Search code examples
javascripttypescriptangular5blogscontentful

Returning contentful Date & Time as a string not number


I am trying to add the blog created time to my blog using contentful and angular 5. I have got the date returning using:

<p> Published: {{blog.fields.createdAt}}</p>

however this returns it like:

Published: 2018-05-17

Where as I would like it to return like:

17th May 2018

The settings on the date and time feature within contentful do not have an option for string formatting. Is there anything I can do in angular that would make this change for me?


Solution

  • Checkout the Angular Docs

    You can use the angular date pipe.

    <p> Published: {{blog.fields.createdAt | date:'dd LLL yyyy'}}</p>
    

    Can't check but that should do it.

    Worth noting that Angular doesn't have a format option for the ordinal (th, nd). You would have to implement something to do that. Possibly creating a custom pipe that implements the DatePipe.