简体   繁体   English

Moment.JS 说日期在 using.format() 时无效,但在使用 isvalid() 时有效 function 我该如何解决这个问题?

[英]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:所以我希望能够让用户输入日期为 DD/MM/YYYY HH:mm 然后将其转换为 MySQL 日期时间格式以便在数据库中输入,所以我运行了moment("20/04/2020 00:19", 'DD/MM/YYYY HH:mm',true).isValid()来测试我是否可以这样做并返回 true 说它是正确的所以实现它但是每当我尝试做任何事情时我都会遇到日期无效错误所以在控制台中再次测试它,它变得有点奇怪:

在此处输入图像描述

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因此,以防万一,其他人有同样的问题 Andrew Li 的评论是正确的,我没有意识到它在 javascript 内置日期格式中无效,所以要解决这个问题,你必须使用

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.因此,这会告诉时刻日期已经采用什么格式,以便它可以成功更改格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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