You must have encountered such a situation that there are two date objects, and then you need to determine whether they are equal. The following method is used to determine:
The Code is as follows:
Var date1 = new Date ("2013-11-29 ");
Var date2 = new Date ("2013-11-29 ");
Console. log (date1 = date2); // false
Here, date1 and date2 look the same, but running date1 = date2 returns false. This is because date1 and date2 are all objects and their types are reference types. Therefore, if you need to compare them, you need to compare their literal values rather than simply using =.
If you want to compare whether two dates are equal, you can write
The Code is as follows:
Var date1 = new Date ("2013-11-29 ");
Var date2 = new Date ("2013-11-29 ");
Console. log (date1.getTime () = date2.getTime (); // true
You can use the getTime () method to return the value corresponding to the date and then compare it.
In fact, there is another way to compare dates.
The Code is as follows:
Var date1 = new Date ("2013-11-29 ");
Var date2 = new Date ("2013-11-29 ");
Console. log (date1-date2 = 0); // true
Here, we use date1 minus date2 to obtain the time difference between them. If it is 0, it must be equal.