综合编程

Validate a date in Nodejs with MomentJs

微信扫一扫,分享到朋友圈

Validate a date in Nodejs with MomentJs
0

I want to validate a date in a NodeJS application.

I tried the MomentsJs
library, but it seems that the isValid()
function ignored all alpha characters, it’s too tolerant for me (When I specify the dateformat as YYYY-MM-DD
, I expect that the date is a 10-characters-string.):

moment("One,2 and 011, 12-10", "YYYY-MM-DD").isValid()  // returns true instead of false
moment("Seppl"); // parsing seems to work with all values?

Is there any way to check dates more strictly?

How can I check if parsing a date fails? (It returns -62167222800000
when printing wrong dates it).

Is there any other better Javascript Data lib that work with different date formats and support date manipulations like MomentJs?

Problem courtesy of: Sonson123

Solution

Moment.js library doesnt provide any method validate the date with given format.

moment(“”, dateFormat) will just check the given date but not with given format.

Following lines from momentjs.com

Note: It is not intended to be used to validate that the input string matches the format string. Because the strictness of format matching can vary depending on the application and business requirements, this sort of validation is not included in Moment.js.

http://momentjs.com/docs/#/parsing/is-valid/

Solution courtesy of: Kiran Nunna

阅读原文...

Node.js Recipes

Spring mvc @requestmapping best practices

上一篇

Kafka 源码解析之 LeaderAndIsr 请求的处理(二十二)

下一篇

您也可能喜欢

评论已经被关闭。

插入图片
Validate a date in Nodejs with MomentJs

长按储存图像,分享给朋友