0

I'm trying to go from moment.js to date-fns, but I'm having issues formatting the dates correctly..

My date looks like this: const convertDate = '2006-03-07T23:00:00.000Z' I'm trying to convert it to: 2006-03-07

In moment.js I did it like this: moment.utc(convertDate).format('YYYY-MM-DD') --> 2006-03-07

In date-fns I tried to do it like this: format(new Date(convertDate), 'yyyy-MM-dd') --> 2006-03-08

I have figured that I somehow need to convert my date to UTC before doing the formatting with date-fns, but it don't seems like this is possible? Any input on this?

alp123
  • 139
  • 4
  • 12
  • 1
    `'2006-03-07T23:00:00.000Z'.substring(0,10)` does the job for me. Or `'2006-03-07T23:00:00.000Z'.split('T')[0]`. Or `'2006-03-07T23:00:00.000Z'.slice(0,10)`. :-) – RobG Oct 21 '21 at 11:50
  • 1
    While just using the substring is a pragmatic approach and will certainly will work for your usecase, check this question [date-fns | How do I format to UTC](https://stackoverflow.com/questions/58561169/date-fns-how-do-i-format-to-utc) Especially look at [this answer](https://stackoverflow.com/a/63227335/3776927) which will allow you to use also different formats (ie not only YYYY-MM-DD from the ISO String) – derpirscher Oct 21 '21 at 12:17

0 Answers0