Comparing dates in JavaScript

-1

Hello, I need to compare the current date with the date entered by the user, but the seconds prevent this check:

var data_publicacao = new Date($('#frm-data').val());
    console.log(data_publicacao);
    var data_atual = new Date();
    console.log(data_atual);
    if (data_publicacao != undefined) {
        if (data_publicacao > data_atual || data_publicacao < data_atual) {
            alert("Sua notícia foi salva, mas ainda não foi publicada! Aguarde.");
        }
        else {
            noticias.push(noticia);
            atualizarLista(noticia);
            alert("Notícia publicada!");
        }
    }

Is there a way to easily take the seconds without leaving the code polluted?

    
asked by anonymous 25.07.2018 / 21:38

3 answers

1

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 ...

    
25.07.2018 / 22:42
1

toLocaleDateString () - Date-only string in the format located on your system.

var data_atual = new Date();

console.log(data_atual.toLocaleDateString());
  

What you want is given in @hkotsubo's answer

     

I'll chew on you!

var data_publicacao = new Date($('#frm-data').val());
data_publicacao.setSeconds(0);
data_publicacao.setMilliseconds(0);
data_publicacao = (data_publicacao.getTime());

var data_atual = new Date();
// mudar segundos e milissegundos para zero
data_atual.setSeconds(0);
data_atual.setMilliseconds(0);
data_atual = (data_atual.getTime());

if (data_publicacao > data_atual || data_publicacao < data_atual) {
.....
    
25.07.2018 / 22:02
-1

I was able to find the answer on GitHub , follow the instructions:

<script src="https://rawgit.com/Lautert/helpers/master/javascript.helpers.js"></script>

var d = new Date();
console.log(d.toDate('dd/mm/yyyy hh:ii:ss'));

console.log(d.toDate('mm/dd/yyyy hh:ii:ss'));

console.log(d.toDate('yyyy-mm-dd'));

console.log(d.toDate('yyyy-dd'));
    
25.07.2018 / 22:31