如何检查日期字符串是否遵循特定的数据格式
我有一个应用程序,用户可以指定自己的数据格式,例如“ MM/DD/YYYY”,“ MM/DD/YY”等
。给定格式。
我尝试使用moment(datastring,dateformat).isvalid()
,但看起来它不能
为datestring提供工作:01/01/20
使用DateFormat mm/dd/yyyy
,期望false
带有dataformat mm/dd/yy
,期望true
> moment('01/01/ 2020 “ mm/dd/yyyy”,true).isvalid() ---期望:true,实际:false
我可以用来实现这一目标的方法?
I have an app, user can specify their own data format, like 'MM/dd/yyyy', 'MM/dd/yy', etc.
Once I receive a date string, I need to validate if the date string is valid for the given format.
I tried to use moment(dataString, dateFormat).isValid()
, but looks it does not do the work
For dateString: 01/01/20
with dateFormat MM/dd/yyyy
, expect false
with dataFormat MM/dd/yy
, expect true
moment('01/01/2020', 'MM/dd/yy').isValid()
--- expect: false, actual: true
moment('01/02/2020', 'MM/dd/yyyy', true).isValid()
--- expect: true, actual: false
What I can use to achieve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您必须将DD大写并在第三个参数中供应真实,以实现严格的解析
you have to capitalize DD and supply true in the 3rd parameter, to enable strict parsing