Search code examples
javascriptreactjsdate-fns

Format date with date-fns


Am trying to format date with such an example 2021-09-20T12:12:36.166584+02:00 with date-fns library but it doesn't work, what's the right approach ?

Below is my code :

import { format } from 'date-fns'
format(date, 'DD.MM.YYYY')

Solution

  • You have to parse your ISO string before you can format it, and use lowercase dd and yyyy:

    import { format, parseISO } from "date-fns";
    
    const dateFormatted = format(parseISO(date), "dd.MM.yyyy");