用moment.js检查日期格式 [英] check format of date with moment.js

查看:2000
本文介绍了用moment.js检查日期格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从我的屏幕中的日历中获取此类型的输入

I am taking input from calendar in my screen which is of this type

DD-MMM-YYYY HH:mm a

但用户可以通过键盘提供日期。现在我必须检查用户是否以正确的格式提供了日期。我在我的应用程序中大量使用moment.js并像这样验证它

but user can provide date from keyboard. Now I have to check whether user has provided the date in correct format or not. I am heavily using moment.js in my application and validating it like this

 if(angular.equals(moment(scope.modelValue).format('DD-MMM-YYYY HH:mm a'), 'Invalid date'))
{
       alert('date is not correct');

}
else
{
alert('date is correct');
}

工作正常,但问题是如果我提供像'18这样的输入 - 2015年2月2'
然后转换为'18 -Feb-2015 00:00 am'。那么现在如何检查格式是否正是我想要的?请帮助..

It is working fine but the problem is if I provide input like '18-Feb-2015 2' then it is converted to '18-Feb-2015 00:00 am'. so now how to check that format is exactly what I want ? please help ..

推荐答案

要查看日期格式,您可以使用:

For checking date format you could use:

moment(checked_date, DATE_FORMAT).format(DATE_FORMAT) === checked_date

这篇关于用moment.js检查日期格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆