Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date-fns is returning incorrect dates

I have a collection of date strings stored in a database in this format:

2018-06-28T14:06:26.524Z

2018-07-02T10:32:18.818Z

2018-07-06T15:08:50.233Z

I need to convert these dates into a format like this on the frontend:

28 June 2018 14:06:26

02 July 2018 10:32:18

06 July 2018 08:50:23

My attempt at doing this using date-fns is:

format(new Date(date), 'dd MMMM yyyy HH:MM:ss')

The problem is that the dates returned from the above are incorrect:

2018-06-28T14:06:26.524Z returns 28 June 2018 15:06:26

2018-07-02T10:32:18.818Z returns 02 July 2018 11:07:18

2018-07-06T15:08:50.233Z returns 06 July 2018 16:07:50

What am I doing wrong here and how to I fix this so the dates are returned correctly?

like image 492
Conor Heena Avatar asked Mar 02 '23 16:03

Conor Heena


2 Answers

You messed up the time:

HH:mm:ss not HH:MM:ss

MM is for month and mm is for minute - docs

like image 157
Adam Avatar answered Mar 04 '23 09:03

Adam


You misspelled HH:MM:ss should be HH:mm:ss look at this list for an explanation on all formats: Date formats

Example:

dd MMMM yyyy HH:mm:ss: 22 August 2006 06:30:07

like image 41
Filip Huhta Avatar answered Mar 04 '23 08:03

Filip Huhta