Search code examples
javascriptreactjsformatdate-fns

Format output date using date-fns with ReactJs


Can you guys help with a custom format output using date-fns react library?

I'm trying to format this date '2022-01-24T05:26:28+0000' to '24 jan 2022, às 02:26', but the 's' is for second, so the output is something like this '24 jan 2022, à28 02:26' Is there a way to prevent the 's' to format?

My code:

{format(
    new Date(post.data.last_publication_date),
    `PP, às p`,
    { locale: ptBR }
)}

I already tried using template literals such as ${`PP, às p`}, but it didn't help.

I'm using ReactJs and NextJs with this library.


Solution

  • So, I read more deep the docs from the library and they have some patterns to edit the output.

    To the date '2022-01-24T05:26:28+0000' be format as '24 jan 2022, às 02:26', it requires some simple quotes, as follows:

    `PP, 'às' p`
    

    So the output will be '24 jan 2022, às 02:26'