When you create a moment
from a date string and pass in the format, moment very loosely checks the date string against the format. for example the following dates are all valid
moment('1','YYYY-MM-DD').isValid() //true
moment('1988-03','YYYY-MM-DD').isValid() //true
moment('is a val1d date!?#!@#','YYYY-MM-DD').isValid() //true
Is there any way to only accept dates that match the specified format?
We can use the startOf method to return a moment date object without the time portion of a date. Also, we can use the format method to return a date string without the time portion of a date.
Compare two dates using Moment.js has a diff() method, which gives the difference between two dates in years, months, days, hours, minutes, seconds, milliseconds, etc. We can use the second unit in our case to find the difference between the two dates.
Date format conversion with Moment is simple, as shown in the following example. moment(). format('YYYY-MM-DD'); Calling moment() gives us the current date and time, while format() converts it to the specified format.
MomentJs recently announced that the library is now deprecated. This is a big deal for the javascript community who actively downloads moment almost 15 million times a week.
As of version 2.3.0, you may specify a boolean for the last argument to make Moment use strict parsing. Strict parsing requires that the format and input match exactly, including delimeters.
moment('It is 2012-05-25', 'YYYY-MM-DD').isValid(); // true
moment('It is 2012-05-25', 'YYYY-MM-DD', true).isValid(); // false
Found in this section of the Moment JS docs
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With