I have 2 dates, one for start and one for end, I try to prevent the start date from being greater than the end date, apart from the fact that I can't do it, I get strange years, days that are not... etc.
console.log("start ", m , d , y);
console.log("fin ", m2 , d2, y2);
console.log("-------");
console.log("start", new Date(m, d, y));
console.log("fin", new Date(m2, d2, y2));
if (new Date(m, d, y) > new Date(m2, d2, y2)) {
format = false;
this.errorServer = true;
this.message = "La fecha de inicio no puede ser mayor que la fecha de fin.";
}
And the result:
As a result of this change, I don't get the error that the start date is greater...
You are using the constructor wrong, the correct thing is:
new Date(year, month, day);
But not only that, but the month starts at 0, then subtract 1:
Not only is the format a bit strange, but it's also always UTC time, so the first line in the console shows me
"2018-11-05T23:00:00.000Z"
why I'm in UTC+1, then the date 2018-11-06 (00:00: 00) has been delayed by one hour.The problem is with the constructor for the date object, which must be new Date(year, monthIndex, day) where monthIndex must be indexed from 0 to 11.
The standard operators
>
,<
,<=
or>=
can normally be used to compare the Date type , however if you wish to use the operators==
,!=
,===
, and!==
you must convert the date to milliseconds using the getTime() method.Official Documentation