Comparing date part only without comparing time in JavaScript

 

Comparing dates
 

const date1 = new Date();
const date2 = new Date('2021-01-23T09:30:24.820Z');

const date1WithoutTime = new Date(date1.getTime());
const date2WithoutTime = new Date(date2.getTime());

//initialize a JavaScript Date to midnight
date1WithoutTime.setUTCHours(0, 0, 0, 0);
date2WithoutTime.setUTCHours(0, 0, 0, 0);

if (date1WithoutTime.getTime() === date2WithoutTime.getTime()) comsole.log('dates are same')

Tags:

Share:

Related posts