用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