If you want to compare the date and time, but ignoring the seconds, an alternative is to use the setSeconds
method and change the value of the seconds to zero.
I recommend changing the milliseconds too, so you guarantee that only the hours and minutes will be considered:
// uma data qualquer
var d = new Date();
// mudar segundos e milissegundos para zero
d.setSeconds(0);
d.setMilliseconds(0);
console.log(d);
Do this with the two dates you want to compare ( data_publicacao
and data_atual
), so you guarantee that seconds (and fractions of seconds) will not interfere with comparison.
To compare them, it's no use using >
and <
(nor any other operators) directly, because it does not always work (if I'm not mistaken, some browsers may work, but it's not guaranteed to work at all ). It is best to use the value returned by getTime()
, which returns the numeric value of the Unix timestamp (the number of milliseconds since 1970- 01-01T00: 00Z).
Then your if
would look like this:
if (data_publicacao.getTime() > data_atual.getTime()
|| data_publicacao.getTime() < data_atual.getTime()) {
Just a detail, this if
means: if the publication date is greater than the current date (that is, in the future) or if the publication date is less than the current date (that is, in the past).
If the publication date is in the past or in the future, you want it to be if
, is that right? That is, any date other than the current one will enter this if
.
Anyway, this is a way of disregarding the seconds in the comparison. I would only revise the if
criterion because it's kinda weird to me ...