skip to Main Content

with asp.net, how can pass javascript date into input with type datetime.

input tag:

<input asp-for="EndingDate"  id="enddate" class="form-control input-sm" />

and the attempt to send javascript date to the input value:

const d = new Date("2015-03-25");
$('#enddate').val(d.toJSON());

3

Answers


  1. Did you try below.

    const d = new Date("2015-03-25");
    $('#enddate').val(d);
    

    This will assign date time to asp.net text filed. But no-masking and calendar. If the field have calendar then you need to call calendar initiate method after this.

       $("#enddate").datepicker();
    

    The form post should work as it is.

    Login or Signup to reply.
  2. Try this

    let endDate = new Date().toISOString().substr(0, 10); //Get Date part
    $('#enddate').attr('value',endDate);
    
    Login or Signup to reply.
  3. If your EndingDate is type of DateTime, it will generate the input with type="datetime-local".

    Change your code below:

    var isoStr = new Date("2015-03-25").toISOString();
    $('#enddate').val(isoStr.substring(0,isoStr.length-1));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search