V
V
Vladislav Linnik2020-05-08 13:09:42
JavaScript
Vladislav Linnik, 2020-05-08 13:09:42

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

3 answer(s)
D
DanKud, 2020-05-08
@VladikLinnik

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) ? 'время в минутах совпадает' : 'время в минутах НЕ совпадает';
*/

I
Igor, 2020-05-08
@loonny

Get a timstamp, remove the milliseconds and seconds part and compare the resulting numbers

S
shsv382, 2020-05-08
@shsv382

date1 = (date1 - (date1%60000))
date2 = (date2 - (date2%60000))
date1 === date2

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question