Answer the question
In order to leave comments, you need to log in
How to compare two dates by minutes?
How to correctly compare minutes of two dates? There are two dates, for example, one is 2020-05-08 12:51 , the other is 2020-05-08 12:51 - they must be equal if one of the dates is 2020-05-08 12:52 - not equal, because for a minute more.
Date.getMinutes() doesn't help, it just returns the number of minutes since the date, and I need to compare them.
Answer the question
In order to leave comments, you need to log in
let date1 = new Date('2020-05-08 12:51').getTime();
let date2 = new Date('2020-05-08 12:52').getTime();
/* переводим timestamp в минуты */
date1 = Math.floor((date1 / 1000 / 60));
date2 = Math.floor((date2 / 1000 / 60));
/* сравниванием минуты */
if (date1 === date2) {
console.log('время в минутах совпадает');
} else {
console.log('время в минутах НЕ совпадает');
}
/* или через тернарный оператор:
(date1 === date2) ? 'время в минутах совпадает' : 'время в минутах НЕ совпадает';
*/
Get a timstamp, remove the milliseconds and seconds part and compare the resulting numbers
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question