Home » Jquery » javascript – How to set input date from ajax laravel

javascript – How to set input date from ajax laravel

Posted by: admin February 22, 2020 Leave a comment

Questions:

I have a date form that I want to fill in the value of ajax response, on the server side I use laravel and the data form becomes like this 2020-02-18 i try with this code, but it doesn’t work. How do I fix it?

$.ajax({
    type: "get",
    url: "{{route('santri-edit-perijinan')}}" + "?id=" + id + "&type=" + type,
    success: function(data) {
        console.log(data.mulai_ijin);
        $("#tanggal_ijin").val(data.mulai_ijin);
    }
}); 
and this my html
  <input type="date" class="form-control" id="tanggal_ijin" name="tanggal">
How to&Answer:

HTML input type=date accept yyyy-MM-dd format as input, So as long as your backend returns the date in the said format, your .val() function won’t have any issue in setting the date.
If your date is being converted in Y-m-d H:i:s format as per PHP formats, you should convert the format using carbon.
Carbon::parse($date)->format('Y-m-d');

PHP date format Y-m-d is equivalent to yyyy-MM-dd format.