0

I am trying to convert the below date and time to dd/mm/yy H:M using javascript.

February 24th 2021, 15:21:46

to

24/02/2021 15:21

I was trying -

moment('February 24th 2021, 15:21:46').format('DD/MM/YYYY HH:mm') 

I am getting an invalid date.

Any help is highly appreciated.

Barmar
  • 669,327
  • 51
  • 454
  • 560
Prithviraj Mitra
  • 9,394
  • 12
  • 51
  • 87

1 Answers1

3

As @Barmer said in their comment, it's not the formatting that is throwing the error.

You should tell moment what format the input is so it can be parsed.

const formattedDate = moment('February 24th 2021, 15:21:46', 'MMMM Do YYYY, HH:mm:ss')
  .format('DD/MM/YYYY HH:mm');
  
console.log(formattedDate);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js" integrity="sha512-qTXRIMyZIFb8iQcfjXWCO8+M5Tbc38Qi5WzdPOYZHIlZpzBHG3L3by84BBBOiRGiEb7KKtAOAs5qYdUiZiQNNQ==" crossorigin="anonymous"></script>
phuzi
  • 9,921
  • 3
  • 25
  • 45