locked
set date in html textbox from ajax RRS feed

  • Question

  • User29410129 posted

    I am trying to set value in html input having type Date. But don't know it's not working here is my code :

    <input type="date" class="form-control" id = "clndr_UpdateStartDate" />

    $.ajax(

    success: function (data)

    {

    var startDate = data[0]['get_start_date'].split(" ");

    $("#clndr_UPdateStartDate").val(startDate);

    });

    Waiting for the positive reply

    Thursday, November 24, 2016 4:57 PM

All replies

  • User-6180675 posted

    check the value of startDate before setting it in date field by using alert(startDate) or consol.log(startDate). From your code it seems that you are passing an array to the val() method and not a single value

    var startDate = data[0]['get_start_date'].split(" ");

    In the above code data[0]['get_start_date'].split(" ") actually returns an array you have to specify the correct index for this array to use that value with val() . Check the code below

    var startDate = data[0]['get_start_date'].split(" "); // this returns an array named startDate
    
    // e.g if data[0]['get_start_date'] = "11/24/2016 12:00:00 AM" then 
    // startDate[0] = "11/24/2016" and  startDate[1] = "12:00:00 AM" 
      
    //I assume you will be using only the date part then you can use the first index like below
    
    $("#clndr_UPdateStartDate").val(startDate[0]);

    Also please post the structure of "data" that you are receiving in your ajax success callback if this doesn't work.

    Thursday, November 24, 2016 6:33 PM
  • User1213248666 posted

    Hi ,

    Your id is not matching, make sure your id is $("#clndr_UPdateStartDate")

    check the below link to set date value.

    http://stackoverflow.com/questions/12346381/set-date-in-input-type-date

    Thursday, November 24, 2016 11:06 PM
  • User-707554951 posted

    Hi DanyalHaider,

    As far as I know, Two problems in this:

    1.Date control in HTML 5 accepts in the format of Year - month - day as we use in SQL

    2.If the month is 9, it needs to be set as 09 not 9 simply. So it applies for day field also.

    Following is an working example, you could refer to it:

      <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js" type="text/javascript"></script>
    <script type = "text/javascript">
    function ShowCurrentTime() {
        $.ajax({
            type: "POST",
          url: "set date in html textbox from ajax.aspx/GetCurrentTime",     
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: OnSuccess,
            failure: function(response) {
        alert(response.d);
    
            }
        });
    }
        function OnSuccess(response) {
            var stringdate = JSON.stringify(response.d);
            var arrydate = stringdate.split("/");
            var month = arrydate[0].substring(1, 3);
            var day = arrydate[1];
            var year = arrydate[2].substring(0, 4);
            var today = year + "-" + month + "-" + (day);
            $("#clndr_UpdateStartDate").val(today);
    }
    </script>
    
    
      <input type="date" class="form-control" id = "clndr_UpdateStartDate" value=""/>
    <input id="btnGetTime" type="button" value="Show Current Time" onclick = "ShowCurrentTime()" />

    CodeBehind:

    [System.Web.Services.WebMethod]
    public static string GetCurrentTime()
            {
                string date= DateTime.Now.ToString();
                return date;
            }

    Output screenshot as below:

    Best regards

    Cathy

    Friday, November 25, 2016 8:49 AM