Skip to content
Advertisement

How to correctly format date with timezone in date-fns?

I am using date-fns library but It is not formatting the date correctly.

My code:

    import { format } from 'date-fns'
    console.log(format(endOfDay(new Date()), "yyyy-MM-dd'T'HH:mm:ssX"))

Required result:

      2022-05-19T23:59:59Z

Actual result:

      2022-05-19T23:59:59+02
        

Please help me in finding the mistake. Thank you

Advertisement

Answer

To get your desired result using format…

import { format } from 'date-fns'

console.log(format(new Date(), "yyyy-MM-dd'T'HH:mm:ss'Z'"));
Advertisement