「jQuery」カレンダーDatepickerの初期日付を設定するサンプル
環境
jquery 3.6.0
Google Chrome 104.0.5112.81
書式
defaultDate: new Date(年,月+!,日)
defaultDateを使ってカレンダーの初期日付を設定します。
引数の月を指定するときは実際の月より-1します。
使用例
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script>
<script>
$(function() {
/*カレンダーの初期日付を設定*/
var regFrom = $("#search").datepicker({
//defaultDateに日付の2022/7/12を指定 2022/8/12を表示
defaultDate: new Date(2022,7,12)
});
});
</script>
</head>
<body>
<!--テキスト入力欄 -->
<p>検索日: <input type="text" id="search" /></p>
</body>
</html>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script>
<script>
$(function() {
/*カレンダーの初期日付を設定*/
var regFrom = $("#search").datepicker({
//defaultDateに日付の2022/7/12を指定 2022/8/12を表示
defaultDate: new Date(2022,7,12)
});
});
</script>
</head>
<body>
<!--テキスト入力欄 -->
<p>検索日: <input type="text" id="search" /></p>
</body>
</html>
<!DOCTYPE html> <html> <head> <link rel="stylesheet" href="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/themes/smoothness/jquery-ui.css"> <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script> <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1/i18n/jquery.ui.datepicker-ja.min.js"></script> <script> $(function() { /*カレンダーの初期日付を設定*/ var regFrom = $("#search").datepicker({ //defaultDateに日付の2022/7/12を指定 2022/8/12を表示 defaultDate: new Date(2022,7,12) }); }); </script> </head> <body> <!--テキスト入力欄 --> <p>検索日: <input type="text" id="search" /></p> </body> </html>
実行結果
「検索日」欄をクリックしたら、カレンダーの初期日付が「2022/08/12」を表示します。