The question is: How does this work after all? How does javascript compare two strings and return true or false depending on the value in them?
When comparing two strings "2" > "14"
, "2" shall be greater than "14" because (in order from left to right) that 2 is greater than 1, and likewise its doubt "Se eu sibstituir a data por '08/05/2019', ainda recebo true"
, is due as above and explained below.
General rule
To calculate string comparisons, Javascript converts each character of a string with its ASCII value. Each character, starting with the left operator, is compared with the corresponding character in the right operator.
In the following example, note that character 7 is less than 8, so as a general rule, regardless of what comes after character 7 of the left operator and after character 8 of the right operator the expression will always be false.
Note that in your case it is a comparison between two strings.
var hoje = new Date(2018, 08, 07).toLocaleDateString();
console.log(typeof hoje); //string
var dataQualquer = '08/05/2018';
console.log(typeof dataQualquer); //string
console.log(hoje); // 07/09/2018
console.log(dataQualquer); //08/05/2018
if (dataQualquer<hoje){
console.log("verdadeiro");
}else{
console.log("falso"); //8 não é menor que 7
}
Note that Javascript does not compare 7 to 8, but its ASCII values
7 corresponde a 055 em ASCII
8 corresponde a 056 em ASCII
NOTE: for numerical values, the results are equal to what you would expect from your school algebra classes.
facil right? For numeric characters the ASCII values start with 0 (048) and end with 9 (057).
So
| cada centena representa o valor ASCII de cada caractere
07/09/2018 | 048 055 047 048 057 047 050 048 049 056
08/05/2018 | 048 056 047 048 053 047 050 048 049 056
---
For alphabetic characters it is not that easy, see the example below
if ("mateus" < "Mateus"){
console.log("verdadeiro");
}else{
console.log("falso");
}
mateus | 109 097 116 101 117 115
Mateus | 077 097 116 101 117 115
---
ASCII values for uppercase letters are smaller than the corresponding ones in minuscule letters.
One of the various solutions, for your case, is to use the order YEAR MONTH DAY
var hoje = new Date(2018, 08, 07).toLocaleDateString();
var dataQualquer = '08/05/2018';
hoje = hoje.split("/").reverse().join("-"); //2018-09-07
dataQualquer = dataQualquer.split("/").reverse().join("-"); // 2018-05-08
if (dataQualquer<hoje){
console.log("verdadeiro");
}else{
console.log("falso");
}
2018-09-07 | 050 048 049 056 045 048 057 045 048 055
2018-05-08 | 050 048 049 056 045 048 053 045 048 056
---
dataQualquer < hoje
givesfalse
and nottrue
– Don't Panic
It works wrong as it makes no sense to compare dates as string in the proposed format. The only real solution is to forget everything in the question code and start right from scratch. If you are going to use strings, use the YYYYMMDD order and do nothing to convert. If the format is already date, compare as date or numerically (using getTime). If they are 2 different formats, convert to a format that makes sense (or string in the right order, or integer/timestamp)
– Bacco
I understand that it works wrong and I know the right way to do it, the question was how the comparison system (>, <, >=, <=) worked between strings. The question of dates was an example in which I came across by chance and used as an example here. But thank you for your attention and attention.
– Máttheus Spoo