javascriptdatedatetimemomentjstempus-dominus-datetimepicker

Moment.JS saying date is invalid when using .format() but is valid when using isvalid() function how can i fix this?


So I want to be able to have the user input a date as DD/MM/YYYY HH:mm then have it converted to MySQL datetime format for input in a database so I ran moment("20/04/2020 00:19", 'DD/MM/YYYY HH:mm',true).isValid() to test if I could do this and got back true to say it is correct so implemented it however I have a date invalid error whenever I try to do anything so tested it again in the console and well it gets a little weird:

enter image description here

Why does this happen it makes no sense to me... and is there a way I can do what I set out to do?


Solution

  • So in case, anyone else has the same issue Andrew Li's comment is correct and I didn't realize it is not valid in javascript built-in date format so to fix this you would have to use

    moment("20/04/2020 00:19","DD/MM/YYYY HH:mm").format('DD-MM-YYYY HH:mm')

    not:

    moment("20/04/2020 00:19").format('DD/MM/YYYY HH:mm')

    This therefore tells moment what format the date is already in so it can successfully change the formatting.