Reputation: 57
My code is this:
const nowDate = moment(new Date()).format("DD/MM/YYYY");
let paraRepDate = '01/01/2021';
let calcParaDate = '30/06/2021';
var x = moment(calcParaDate).isBefore(nowDate)
console.log(x) // false
How is it possible?
Upvotes: 0
Views: 1137
Reputation: 8531
Default format of momentjs is MM/DD/YYYY
. In your solution you can see warning information in console:
Deprecation warning: value provided is not in a recognized RFC2822 or ISO format. moment construction falls back to js Date(), which is not reliable across all browsers and versions. Non RFC2822/ISO date formats are discouraged. Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
You have to specify format moment(date, format)
. Something like this:
const format = 'DD/MM/YYYY';
const nowDate = moment();
const calcParaDate = '30/06/2021';
const isBefore = moment(calcParaDate, format).isBefore(nowDate)
console.log(isBefore);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment-with-locales.min.js" integrity="sha512-LGXaggshOkD/at6PFNcp2V2unf9LzFq6LE+sChH7ceMTDP0g2kn6Vxwgg7wkPP7AAtX+lmPqPdxB47A0Nz0cMQ==" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
Also you don't have to pass new Date()
in moment()
for current datetime.
Upvotes: 2