javascript - Moment.js ignores day of week during parsing validation -
i trying check string being valid date string. can't validate day of week in string representation.
these 2 commands show similar result:
moment("tuesday 19/09/2017", "dddd d/m/yyyy", true).isvalid() //true moment("tuesday 20/09/2017", "dddd d/m/yyyy", true).isvalid() //true
how can check it? can't split string in 2 parts, because don't know format.
it appear isvalid checking date part.
if want check whole string, maybe 1 idea parse it, if result same after re-formating it's valid.
eg.
function valid(dt,fmt) { return moment(dt, fmt).format(fmt) === dt; } let fmt = 'dddd d/m/yyyy'; let dates = [ "tuesday 19/9/2017", "tuesday 20/9/2017" ]; dates.foreach((dt) => { console.log(dt + ' ' + valid(dt, fmt)); });
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment.min.js"></script>
Comments
Post a Comment