The problem is that format
returns a string, and strings are compared lexicographically, ie even digits like 0
and 1
are treated as characters, and therefore a string that starts with 0
is considered "smaller" than one starting with 1
(you can read here to better understand this behavior).
In the case of Moment.js, isAfter
accepts strings as parameter, and it tries to do the Parsing, but as you passed a string in "dd/mm/yyyy" format and it is not guaranteed that this will be recognized, it fails to do the comparison correctly (from documentation: "Browser support for Parsing strings is inconsistent. Because there is no Specification on which formats should be supported, what Works in some browsers will not work in other browsers").
Obs: in my test, it gave error instead of returning true
, but anyway it should not be done this way.
If you want to compare dates, do not string them. In this case, use the objects themselves moment
:
var dataAtual = moment(); // data atual
var dataVencimento = moment('2021-02-01');
console.log(dataAtual.isAfter(dataVencimento, 'day')); // false
<script src="https://momentjs.com/downloads/moment.min.js"></script>
In case I used 'day'
as the second parameter so that it does not take into account the timetable (see documentation), because I understood that you only compare the day, month and year, regardless of the time. This is necessary because the current date/time will also have the time and without the second parameter, this is also taken into account in the comparison.
Just for the record, your mistake was confusing dates with formats. But understand one thing: as I said before here, here and here, dates have no format.
A date is just a concept, an idea: it represents a specific point in the calendar.
The date of "1 January 1970", for example, represents this: the specific point of the calendar that corresponds to the first day of January 1970. To express this idea in text form, I can write it in different ways:
- 01/01/1970 (a common format in many countries, including Brazil)
- 1/1/1970 (American format, reversing day and month)
- 1970-01-01 (the format ISO 8601)
- First of January 1970 (in good Portuguese)
- January 1st, 1970 (in English)
- 1970 年 1 月 1 日 (in Japanese)
- and many others...
Note that each of the above formats is different, but all represent the same date (the same numerical values of the day, month and year).
In this case, an object moment
represents a date, and format
returns a string representing this date in some format.
If you want to compare dates, don’t use strings, use the dates themselves.
But just out of curiosity, a format that allows a direct comparison without these problems is defined by ISO 8601:
var dataAtual = moment().format('YYYY-MM-DD'); // data atual
var dataVencimento = moment('2021-02-01').format('YYYY-MM-DD');
console.log(dataAtual > dataVencimento); // false
<script src="https://momentjs.com/downloads/moment.min.js"></script>
But of course, if you already have the dates, it makes no sense to turn them into strings to compare them, and the dates themselves can already be compared directly.
Thanks for the excellent reply @hkotsubo! It’s very clear now! Hug!
– Jalber Romano