Validate a date in Nodejs with MomentJs

综合编程 2018-06-25

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


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

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.

Solution courtesy of: Kiran Nunna

Node.js Recipes

责编内容by:Node.js Recipes (源链)。感谢您的支持!


性能优化 – 让nodejs再快一点 本文首次发表于 北斗同构github , 转载请注明出处 前言 很多前端工程师在做页面性能调优的过程中,极少关注代码本身的执行效率,更多关注的是网络...
Returning Data Asynchronously from Model Layer in ... I'm working on a node application and have structured it with a separate mode...
快速定位线上 Node.js 内存泄漏问题 背景 目前容器化和微服务是服务端开发的一个潮流和趋势,然而在这种微服务的架构下,我们在实际的企业开发中会遇到一些困境:趋向于越来越稳定的服务端 API...
关于前后端同构,我的一点思路和心得(vue、nodejs、react、模版、amd)... 最近1年多,前后端同构慢慢变成一个流行词,也许很多人还停留在前后端分离的最佳实践道路上,但实际上又有一批人已经从简单的服务端渲染走向探索最佳前后端同构方案...
node.js utility library for working with objects a... Is there good utility library for working with objects and arrays. For...