tkuchikiの日記

新ブログ https://blog.tkuchiki.net

jQueryUI datepickerに時間設定機能をプラスしたtimepickerの設定方法

http://trentrichardson.com/examples/timepicker/
が作者様のページです。

githubなどからソースコードをダウンロードして設置。

haedタグ内に以下を記述(パスは適宜変更)。

<link rel="stylesheet" href="http://ajax.googleapis.com/ajax/libs/jqueryui/1/themes/ui-lightness/jquery-ui.css" />
<link href="/css/jquery-ui-timepicker-addon.css" rel="stylesheet" type="text/css" />

<script src="http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="http://ajax.googleapis.com/ajax/libs/jqueryui/1/jquery-ui.min.js"></script>
<script type="text/javascript" src="/js/jquery-ui-timepicker-addon.js"></script>
<script type="text/javascript" src="/js/jquery-ui-sliderAccess.js"></script>
<script type="text/javascript" src="/js/localization/jquery-ui-timepicker-ja.js"></script>
<script>
    $(function(){
        $(".datepicker").datetimepicker({
            addSliderAccess: true,
            sliderAccessArgs: { touchonly: false },
            changeMonth: true,
            changeYear: true
        });
    });
</script>


ただ、これだけだとdatepickerの日本語化が行われない。
そこで、/js/localization/jquery-ui-timepicker-ja.js を編集。

(function($) {
    $.timepicker.regional['ja'] = {
        timeOnlyTitle: '時間を選択',
        timeText: '時間',
        hourText: '時',
        minuteText: '分',
        secondText: '秒',
        millisecText: 'ミリ秒',
        timezoneText: 'タイムゾーン',
        currentText: '現時刻',
        closeText: '閉じる',
        timeFormat: 'HH:mm',
        amNames: ['午前', 'AM', 'A'],
        pmNames: ['午後', 'PM', 'P'],
        isRTL: false,
+        prevText: '&#x3C;前',
+        nextText: '次&#x3E;',
+        monthNames: ['1月','2月','3月','4月','5月','6月',
+        '7月','8月','9月','10月','11月','12月'],
+        monthNamesShort: ['1月','2月','3月','4月','5月','6月',
+        '7月','8月','9月','10月','11月','12月'],
+        dayNames: ['日曜日','月曜日','火曜日','水曜日','木曜日','金曜日','土曜日'],
+        dayNamesShort: ['日','月','火','水','木','金','土'],
+        dayNamesMin: ['日','月','火','水','木','金','土'],
+        weekHeader: '週',
+        dateFormat: 'yy-mm-dd',
+        firstDay: 0,
+        showMonthAfterYear: true,
+        yearSuffix: '年'
    };
    $.timepicker.setDefaults($.timepicker.regional['ja']);
})(jQuery);