简体   繁体   中英

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:

在此处输入图像描述

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM