-1

I want to compare two dates using this code

  let dob=new Date(this.dob);  
  if(birthDate.getDate()==new Date("1990-01-01").getDate())
  {
    console.log('date is equal');
  }
  else 
  {
    console.log('date is not equal');
  }

but it shows date is equal when i have 2022-08-01 this date in birthDate variable. Any solution Thanks

user3653474
  • 3,393
  • 6
  • 49
  • 135
  • 1
    Does this answer your question? [Checking if two Dates have the same date info](https://stackoverflow.com/questions/4428327/checking-if-two-dates-have-the-same-date-info) – Bhaumik Pandhi Aug 02 '22 at 05:56

2 Answers2

3

The getDate method of Date returns the day of the date (in both cases you gave as an exampel, this is 01). To compare between dates, you can use the getTime method (returns the number of milliseconds passed since Jan 1st, 1970)

getDate documentation

raz-ezra
  • 514
  • 1
  • 15
0

you can use momentjs like this

var bool2 = moment('2019-10-20').isSame('2019-10-20'); //true
console.log(bool2);
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.4/moment.min.js"></script>