EnvelopedDevil
EnvelopedDevil

Reputation: 658

cannot set date input value using JavaScript

I am still not that experienced with HTML and JavaScript but I have been stumbled on this one and have no clue why does it not work.

I have the following code

function DateConverter(date) {
  let dateArray = date.split(".");
  let dateFormatted = dateArray[1] + "-" + dateArray[0] + "-" + dateArray[2];
  return dateFormatted;
}

//ajax for LoadingDataInDetais
$("#taskList").change(function(event) {
  let a = $(this).val();
  $.ajax({
    url: "/Taskview?handler=GrabTask&taskId=" + $(this).val(),
    type: "GET",
    dataType: "html",
    success: function(data, textStatus, XMLHttpRequest) {
      var task = JSON.parse(data);
      $('#task_City').val(task["name"]);
      $('#task_Location').val(task["loc"]);
      $('#task_Client').val(task["client"]);
      $('#task_Number').val(task["taskNo"]);
      $('#task_Date').val(DateConverter(task["date"]));
    }
  });
});
<!--Other fields above-->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="form-group col-md-8 date" data-provide="datepicker">
  <label class="control-label">Date</label>
  <input id="task_Date" type="date" class="form-control" placeholder="Date" />
</div>

All other fields have their values set correctly but the 'date' one does not and I have no clue why. I even tried with plain java script but it just doesn't work. The console doesn't say anything.

The date from the ajax comes in the format "dd.MM.yyyy" and I tried converting it to some other format but all to no avail

Can someone shed some light into the problem;

Upvotes: 2

Views: 1671

Answers (1)

Simon Franzen
Simon Franzen

Reputation: 2727

You are returning the wrong values in your formatter:

function DateConverter(date) {
    return date.split('.').reverse().join('-');
}

Upvotes: 3

Related Questions