Set date in input type date Ask Question

Set date in input type date Ask Question

I will set today's date in the datepicker input type date in Chrome.

$(document).ready(function() {
    let now = new Date();
    let today = now.getDate()  + '/' + (now.getMonth() + 1) + '/' + now.getFullYear();
    console.log(today);
    $('#datePicker').val(today);
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="datePicker" type="date">

However, it is not working.

Please try the code snippet in Chrome.

ベストアンサー1

Fiddle link : http://jsfiddle.net/7LXPq/93/

Two problems in this:

  1. HTML 5の日付コントロールは、SQLで使用される年-月-日の形式を受け入れます。
  2. 月が 9 の場合、単純に 9 ではなく 09 に設定する必要があります。これは日フィールドにも同様に適用されます。

デモについては、次のフィドル リンクに従ってください:

var now = new Date();

var day = ("0" + now.getDate()).slice(-2);
var month = ("0" + (now.getMonth() + 1)).slice(-2);

var today = now.getFullYear()+"-"+(month)+"-"+(day) ;

$('#datePicker').val(today);

おすすめ記事