JavaScript code for showing yesterday’s date and todays date

<script>


var date = new Date();
var showDate;

function listNavigate(ctrl=""){ 
if(ctrl == "prev"){
    date.setDate(date.getDate() - 1);
    showDate=date.getFullYear()+"-"+parseInt(date.getMonth()+1).toString().padStart(2, "0")+"-"+date.getDate().toString().padStart(2, "0");
    $('#showDate').val(date.getDate().toString().padStart(2, "0")+"-"+parseInt(date.getMonth()+1).toString().padStart(2, "0")+"-"+date.getFullYear());
  }else if(ctrl == "next")
{
    date.setDate(date.getDate() + 1);
    showDate=date.getFullYear()+"-"+parseInt(date.getMonth()+1).toString().padStart(2, "0")+"-"+date.getDate().toString().padStart(2, "0");
    $('#showDate').val(date.getDate().toString().padStart(2, "0")+"-"+parseInt(date.getMonth()+1).toString().padStart(2, "0")+"-"+date.getFullYear());
}else{
    showDate=date.getFullYear()+"-"+parseInt(date.getMonth()+1).toString().padStart(2, "0")+"-"+date.getDate().toString().padStart(2, "0");
    $('#showDate').val(date.getDate().toString().padStart(2, "0")+"-"+parseInt(date.getMonth()+1).toString().padStart(2, "0")+"-"+date.getFullYear());
}
var form = new FormData();
var settings = {
  "url": "/api/get-xxxxx-by-date/"+showDate+"/"+localStorage.language,
  "method": "GET",
  "timeout": 0,
  "headers": {
    
  },
  "processData": false,
  "mimeType": "multipart/form-data",
  "contentType": false,
  "data": form
};

$.ajax(settings).done(function (response) {
  //console.log(response);
  const obj = JSON.parse(response);
  var data=obj['data'];
  for(i in data)
  {
    var sun=data[i]['sun'].split('-');
    var moon=data[i]['moon'].split('-');
    var Sunrise=sun[0];
    var Sunset=sun[1];
    var Moonrise=moon[0];
    var Moonset=moon[1];
 $("#Sunrise").html(Sunrise);
 $("#Sunset").html(Sunset);
 $("#Moonrise").html(Moonrise);
 $("#Moonset").html(Moonset);
 $("#Body").html(data[i]['description']);
  }
  $("#showDate").html(showDate);
 
});

$(document).ready(function(){
                $(document).ajaxSuccess(function(){
                  
                });
                $(document).ajaxError(function (event, jqxhr, settings) {                  
                    if (settings.url == "") {
         
                    }
                });  
                
            });

}

$("#showDate")
        .datepicker({
          autoclose: true,
       todayHighlight: true,
       format: 'dd-mm-yyyy',
            onSelect: function(dateText) {
                console.log("Selected date: " + dateText + "; input's current value: " + this.value);
		     //   $(this).change();
		    }
		}).on("change", function() {
           day=this.value.split("-")[0];
           month=this.value.split("-")[1];
           year=this.value.split("-")[2];
           newdate=year+"-"+month+"-"+day;
            date = new Date(newdate);            

listNavigate();
        });

listNavigate();
</script>

Leave a Reply