Home > front end >  how to retrieve date and show in input (type="Date") using ajax
how to retrieve date and show in input (type="Date") using ajax

Time:12-01

I'm using this code to retrieve data from the database by using ajax all data was returned successfully but the date was not shown in the input (type="date") I have done a lot of research and implementation but still have not succeeded follo

function EditHoliday(id) {
        debugger;
        var url = "/RoketChat/editHoliday?Id="   id;
        $("#editHolidays").modal();
        $.ajax({
            type: "GET",
            url: url,
            success: function (data) {
                var obj = JSON.parse(data);
                $("#id").val(obj.Id);
                $("#HolidayName").val(obj.Name); 
                $("#StartDate").val(obj.StartDate);
                $("#EndDate").val(obj.EndDate);
            }
        })
    }

CodePudding user response:

by using following Code we can retrieve the date and show it in input type="date"

 const a = new Date(obj.StartDate);
                const formattedDate = a.getFullYear()   '-'   ("0"   (a.getMonth()   1)).slice(-2)   '-'   ("0"   a.getDate()).slice(-2)
                $("#StartDate").val(formattedDate);

                const b = new Date(obj.EndDate);
                const formattedEndDate = b.getFullYear()   '-'   ("0"   (b.getMonth()   1)).slice(-2)   '-'   ("0"   b.getDate()).slice(-2)
                $("#EndDate").val(formattedEndDate);
  • Related