How to extract values from HTML <input type=“date”> using jQuery

后端 未结 5 1367
无人及你
无人及你 2020-12-16 12:38

Using an HTML input type=\"date\" and a submit button. I would like to populate the variables day, month, and year with t

5条回答
  •  悲&欢浪女
    2020-12-16 12:49

    Firstly you need to create a Date object from input element value. And then you will be able to get day, month and year from this object.

    $('#submit').on('click', function(){
      var date = new Date($('#date-input').val());
      day = date.getDate();
      month = date.getMonth() + 1;
      year = date.getFullYear();
      alert([day, month, year].join('/'));
    });
    

    Working example: https://jsfiddle.net/8poLtqvp/

提交回复
热议问题