Search code examples
javascriptdatedate-fns

Date-fns format function shows next date instead of the expected one


I was trying to format a date from ISO using date-fns library and it's giving the next day as the output.

format(new Date("2021-03-16T23:42:37Z"), "MMM do"); 
// Output: Mar 17th

I am expecting to get Mar 16. Can anyone please let me know what I'm doing wrong in here?


Solution

  • The trailing ‘Z’ is UTC but the Date instance gets created in your local time zone. 23:42 in UTC is after midnight where you are.