<!doctype html> <html class="no-js"> <head> <meta charset="utf-8"> <meta http-equiv="X-UA-Compatible" content="IE=edge"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>日期组件</title> <link rel="stylesheet" href="assets/css/amazeui.min.css"> <!--[if (gte IE 9)|!(IE)]><!--> <script src="assets/js/jquery.min.js"></script> <!--<![endif]--> <!--[if lte IE 8 ]> <script src="assets/ie8/jquery.min.js"></script> <script src="assets/ie8/modernizr.js"></script> <script src="assets/js/amazeui.ie8polyfill.min.js"></script> <![endif]--> <script src="assets/js/amazeui.min.js"></script> </head> <body style=" padding: 50px;"> <!--基本形式--> <form action="" class="am-form" data-am-validator> <p> <input type="text" class="am-form-field" placeholder="日历组件" data-am-datepicker readonly required /> </p> <p><button class="am-btn am-btn-primary">提交</button></p> </form> <!--结合组件使用--> <div class="am-input-group am-datepicker-date" data-am-datepicker="{format: 'dd-mm-yyyy'}"> <input type="text" class="am-form-field" placeholder="日历组件" readonly> <span class="am-input-group-btn am-datepicker-add-on"> <button class="am-btn am-btn-default" type="button"><span class="am-icon-calendar"></span> </button> </span> </div> <!--更改颜色--> <p><input type="text" class="am-form-field" placeholder="日历组件" data-am-datepicker="{theme: 'success'}" readonly/></p> <!--视图模式--> <div class="am-input-group am-datepicker-date" data-am-datepicker="{format: 'yyyy-mm-dd', viewMode: 'years'}"> <input type="text" class="am-form-field" placeholder="日历组件" readonly> <span class="am-input-group-btn am-datepicker-add-on"> <button class="am-btn am-btn-default" type="button"><span class="am-icon-calendar"></span> </button> </span> </div><br> <!--只能选择年份--> <div> <input type="text" class="am-form-field" data-am-datepicker="{format: 'yyyy ', viewMode: 'years', minViewMode: 'years'}" placeholder="日历组件" data-am-datepicker readonly/> </div><br> <!--自定义事件--> <div class="am-alert am-alert-danger" id="my-alert" style="display: none"> <p>开始日期应小于结束日期!</p> </div> <div class="am-g"> <div class="am-u-sm-6"> <button type="button" class="am-btn am-btn-default am-margin-right" id="my-start">开始日期</button><span id="my-startDate">2014-12-20</span> </div> <div class="am-u-sm-6"> <button type="button" class="am-btn am-btn-default am-margin-right" id="my-end">结束日期</button><span id="my-endDate">2014-12-25</span> </div> </div> <script> $(function() { var startDate = new Date(2014, 11, 20); var endDate = new Date(2014, 11, 25); var $alert = $('#my-alert'); $('#my-start').datepicker(). on('changeDate.datepicker.amui', function(event) { if (event.date.valueOf() > endDate.valueOf()) { $alert.find('p').text('开始日期应小于结束日期!').end().show(); } else { $alert.hide(); startDate = new Date(event.date); $('#my-startDate').text($('#my-start').data('date')); } $(this).datepicker('close'); }); $('#my-end').datepicker(). on('changeDate.datepicker.amui', function(event) { if (event.date.valueOf() < startDate.valueOf()) { $alert.find('p').text('结束日期应大于开始日期!').end().show(); } else { $alert.hide(); endDate = new Date(event.date); $('#my-endDate').text($('#my-end').data('date')); } $(this).datepicker('close'); }); }); </script> <!--设置禁止选择日期--> <div class="am-g"> <div class="am-u-sm-6"> 设置禁用日期<br/> <p><input type="text" class="am-form-field" placeholder="今天之前的日期被禁用" id="my-start-2"/></p> </div> <div class="am-u-sm-6"> 禁用日期<br/> <p><input type="text" class="am-form-field" id="my-end-2" /></p> </div> </div> <script> $(function() { var nowTemp = new Date(); var nowDay = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), nowTemp.getDate(), 0, 0, 0, 0).valueOf(); var nowMoth = new Date(nowTemp.getFullYear(), nowTemp.getMonth(), 1, 0, 0, 0, 0).valueOf(); var nowYear = new Date(nowTemp.getFullYear(), 0, 1, 0, 0, 0, 0).valueOf(); var $myStart2 = $('#my-start-2'); var checkin = $myStart2.datepicker({ onRender: function(date, viewMode) { // 默认 days 视图,与当前日期比较 var viewDate = nowDay; switch (viewMode) { // moths 视图,与当前月份比较 case 1: viewDate = nowMoth; break; // years 视图,与当前年份比较 case 2: viewDate = nowYear; break; } return date.valueOf() < viewDate ? 'am-disabled' : ''; } }).on('changeDate.datepicker.amui', function(ev) { if (ev.date.valueOf() > checkout.date.valueOf()) { var newDate = new Date(ev.date) newDate.setDate(newDate.getDate() + 1); checkout.setValue(newDate); } checkin.close(); $('#my-end-2')[0].focus(); }).data('amui.datepicker'); var checkout = $('#my-end-2').datepicker({ onRender: function(date, viewMode) { var inTime = checkin.date; var inDay = inTime.valueOf(); var inMoth = new Date(inTime.getFullYear(), inTime.getMonth(), 1, 0, 0, 0, 0).valueOf(); var inYear = new Date(inTime.getFullYear(), 0, 1, 0, 0, 0, 0).valueOf(); // 默认 days 视图,与当前日期比较 var viewDate = inDay; switch (viewMode) { // moths 视图,与当前月份比较 case 1: viewDate = inMoth; break; // years 视图,与当前年份比较 case 2: viewDate = inYear; break; } return date.valueOf() <= viewDate ? 'am-disabled' : ''; } }).on('changeDate.datepicker.amui', function(ev) { checkout.close(); }).data('amui.datepicker'); }); </script> <!--扩展语言--> <p> <input type="text" class="am-form-field" placeholder="来一丢丢 French" data-am-datepicker="{locale: 'fr', autoClose: 0}" readonly/> </p> <script> (function($) { $.AMUI && $.AMUI.datepicker && ($.AMUI.datepicker.locales.fr = { days: ["Dimanche", "Lundi", "Mardi", "Mercredi", "Jeudi", "Vendredi", "Samedi", "Dimanche"], daysShort: ["Dim", "Lun", "Mar", "Mer", "Jeu", "Ven", "Sam", "Dim"], daysMin: ["D", "L", "Ma", "Me", "J", "V", "S", "D"], months: ["Janvier", "Février", "Mars", "Avril", "Mai", "Juin", "Juillet", "Août", "Septembre", "Octobre", "Novembre", "Décembre"], monthsShort: ["Jan", "Fev", "Mar", "Avr", "Mai", "Jui", "Jul", "Aou", "Sep", "Oct", "Nov", "Dec"], weekStart: 1 }); })(window.jQuery); </script> </body> </html>