Sunday, 30 June 2024

Fill date in input type date using javascript

<input type="date" name="DOB"  id="dob" placeholder="Date of Birth  " >

Input type date takes date in format of yyyy-mm-dd. But it will show you as per your local clock setting.

While editing A form we need to fill the date coming from the database into the date picker. You will have to change format yyyy-mm-dd otherwise date will not be show .

My date formate coming from the database is 15-05-1985
 <input type="hidden" id="ddob" value="@Model.DOB" />

var dt = new Date($("#ddob").val().split("/").reverse().join("/"));

        var td = dt.getFullYear() + "-";

        month = (dt.getMonth() + 1);

        if (month < 10)

            td += "0" + month;

        else

            td += month;

        td += "-";

        day = dt.getDate();

        if (day < 10)

            td += "0" + day;

        else

            td += day;       

        $("#dob").val(td);


Next Topic

No comments:

Post a Comment