2

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;

4
  • 2
    jsfiddle.net/7LXPq/93 this may help Commented Mar 20, 2018 at 13:19
  • @Developer so the problem is in the format as it should be yyyy-MM-dd? Commented Mar 20, 2018 at 13:22
  • @Developer it helper. Post it as an answer so I can accept it Commented Mar 20, 2018 at 13:23
  • javascriptisnotjava.io Commented Mar 20, 2018 at 15:45

1 Answer 1

3

You are returning the wrong values in your formatter:

function DateConverter(date) {
    return date.split('.').reverse().join('-');
}
Sign up to request clarification or add additional context in comments.

1 Comment

Oneliner feels more expressive: return date.split('.').reverse().join('-');

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.