load ajax from a date

1

I need a data to be updated in ajax but depending on the selected date, for example, if the date is less than today it should show a div showing something, and if it exceeds today's date it should show another div, currently only handle basic ajax, I attach the basic example (the official example) of ajax that is what is used, can someone help me with this?

function loadDoc() {
  var xhttp = new XMLHttpRequest();
  xhttp.onreadystatechange = function() {
    if (this.readyState == 4 && this.status == 200) {
     document.getElementById("demo").innerHTML = this.responseText;
    }
  };
  xhttp.open("GET", "ajax_info.txt", true);
  xhttp.send();
}
<!DOCTYPE html>
<html>
<body>

<div id="demo">
  <h2>Let AJAX change this text</h2>
  <button type="button" onclick="loadDoc()">Change Content</button>
</div>

</body>
</html>
    
asked by Sergio 17.01.2018 в 20:52
source

2 answers

0

I would recommend working with Jquery so that the use of Ajax would be easier for you when making your requests.

function loadDoc(){
  var fecha = "17/01/18";
 $("#demo").text(fecha);//Para cambiar el texto del h2 solo demostracion
  $.get( "youdomain/serviceName", { date: fecha } )
  .done(function( data ) {
    $("#demo").text(data);
  });
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.0/jquery.min.js"></script>
<!DOCTYPE html>
<html>
<body>
<div>
  <h2 id="demo">Let AJAX change this text</h2>
  <button type="button" onclick="loadDoc()">Change Content</button>
</div>
</body>
</html>
    
answered by 17.01.2018 в 21:37
0

I recommend you use this: (If that is the case)

    <script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.20.1/moment.min.js">
     var fecha1 = moment();//eso es la fecha de hoy en segundos
                var fecha2 = moment(la variable de la fecha que mencionaste);//eso la fecha convertida en segundos
                var difdias = fecha1.diff(fecha2, 'days');//esto resta 
                        ambas y nos devuelve la cantidad de dias de diferencia,luego haz un if (si el numero difdias es negativo es que la fecha es superior a la de hoy con eso y lo que puso JaimeAtriano yo creo que podrias hacerlo)
    
answered by 17.01.2018 в 22:04