mui 时间控件动态设置开始时间和结束时间(jsp)

jsp 页面标签

<link rel="stylesheet" href="${pageContext.request.contextPath}/hyres/css/mui.min.css">
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/hyres/css/mui.picker.min.css" />

<script src="${pageContext.request.contextPath}/hyres/js/mui.min.js"></script>
<script src="${pageContext.request.contextPath}/hyres/js/mui.picker.min.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/weui/lib/jquery-2.1.4.js"></script>


<div id = "months"  style="display: none">
	<div class="mui-input-row date">
		<button onclick="dtpickerM($(this));" class="btn start_btn"></button>
		<input id='result_start_m' class="start" value="" readonly="readonly" placeholder="开始月份"/>
	</div>
	<div class="mui-input-row date">
		<button onclick="dtpickerM($(this));"  class="btn start_btn"></button>
		<input id='result_end_m' class="start" value=""  readonly="readonly" placeholder="结束月份"/>
	</div>
</div>
<div id ="dates">
	<div class="mui-input-row date">
		<button  onclick="dtpickerD($(this));" class="btn start_btn"></button>
		<input id='result_start_d' class="start" value=""  readonly="readonly" placeholder="开始日期"/>
	</div>
	<div class="mui-input-row date">
		<button onclick="dtpickerD($(this));" class="btn start_btn"></button>
		<input id='result_end_d' class="start" value=""  readonly="readonly" placeholder="结束日期"/>
	</div>
</div>

js 代码

function dtpickerD (element){
	var dtpicker = null;
	var id = element.next().attr('id')
		if(id.indexOf('start')>0){ //选择的开始时间
			$("#result_start_d").val("");
			$("#result_end_d").val("");
		}
	var start_d =  $("#result_start_d").val(); //开始日期
	var end_d =  $("#result_end_d").val();//结束日期
	if(id.indexOf('end')>0 && start_d =="" ){
		layer.msg("请先选择开始日期");
		return;
	}
	if(start_d != ""){
		//var new_start_d =  start_d.replace("-",",");
		var start_date = new Date(start_d); //将开始日期转换成Date类型
		var end_date = new Date(start_date);
		//判断具体加几天
		var now = new Date();
		var day; //加的月数
		var intervalDay = Math.floor((now.getTime() - start_date.getTime())/(24*3600*1000));
		if(intervalDay > 6){
			day = 6;
		}else{
			day = intervalDay;
		}
		
		end_date.setDate(start_date.getDate()+day);  
		//var end_NYR = end_date.getFullYear()+"-"+(end_date.getMonth()+1+"-"+end_date.getDate());
		 dtpicker = new mui.DtPicker({
			    type: "date",//设置日历初始视图模式 
			    beginDate:start_date,//设置开始日期 
			    endDate: end_date,//设置结束日期 
			})
		
	 }else{
		 dtpicker = new mui.DtPicker({
		    type: "date",//设置日历初始视图模式 
		    //beginDate: new Date(2015, 04),//设置开始日期 
		    endDate: new Date(),//设置结束日期 
		})
	 }
	 	dtpicker.show(function(e) {
		 element.next().val(e.value);
	})
	 
 }
 
 function dtpickerM (element){
	var dtpicker = null;
	
	var id = element.next().attr('id')
		if(id.indexOf('start')>0){ //选择的开始时间
			$("#result_start_m").val("");
			$("#result_end_m").val("");
		}
	var start_m =  $("#result_start_m").val(); //开始月份
	var end_m =  $("#result_end_m").val();//结束月份
	if(id.indexOf('end')>0 && start_m =="" ){
		layer.msg("请先选择开始月份");
		return;
	}
	if(start_m != ""){
		var start_date = new Date(start_m); //将开始月份转换成Date类型
		var end_date = new Date(start_date);
		//判断具体加几个月份
		var now = new Date();
		var day; //加的月数
		var intervalMonth = (now.getFullYear()*12+now.getMonth()) - (start_date.getFullYear()*12+start_date.getMonth());
		if(intervalMonth > 5){
			day = 5;
		}else{
			day = intervalMonth;
		}
		
		end_date.setMonth(start_date.getMonth()+day);  
		//var end_NY = end_date.getFullYear()+"-"+(end_date.getMonth()+1);
		 dtpicker = new mui.DtPicker({
			    type: "month",//设置日历初始视图模式 
			    beginDate: start_date,//设置开始日期 
			    endDate: end_date,//设置结束日期 
			})
		
	 }else{
		 dtpicker = new mui.DtPicker({
		    type: "month",//设置日历初始视图模式 
		    //beginDate: new Date(2015, 04),//设置开始日期 
		    endDate: new Date(),//设置结束日期 
		})
	 }
	 	dtpicker.show(function(e) {
		 element.next().val(e.value);
	})
 }

实现的功能是开始到结束月份的跨度<=6 个月,开始到结束日期的跨度是<=7天,比如选择了开始月份为 2019-02,那么选择结束月份的时候,时间控件只会提供2019-02 — 2019-07 这6个月份以供选择。

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
HTML5+ 的 pickDate 和 MUI 的 dtpicker 都是移动端时间控件,它们的使用和对比如下: 1. 使用方式: HTML5+ 的 pickDate 使用方式: ```html <input type="date" id="datePicker" /> ``` MUI 的 dtpicker 使用方式: ```javascript var dtPicker = new mui.DtPicker({ type: "date", // 显示类型,可选值:date、datetime、time beginDate: new Date(2019, 0, 1), // 开始日期 endDate: new Date(2022, 11, 31), // 结束日期 labels: ["年", "月", "日", "时", "分"], // 按钮上的文字 customData: { h: [ { value: "0", text: "0时" }, { value: "1", text: "1时" }, { value: "2", text: "2时" }, { value: "3", text: "3时" }, { value: "4", text: "4时" }, { value: "5", text: "5时" }, { value: "6", text: "6时" }, { value: "7", text: "7时" }, { value: "8", text: "8时" }, { value: "9", text: "9时" }, { value: "10", text: "10时" }, { value: "11", text: "11时" }, { value: "12", text: "12时" }, { value: "13", text: "13时" }, { value: "14", text: "14时" }, { value: "15", text: "15时" }, { value: "16", text: "16时" }, { value: "17", text: "17时" }, { value: "18", text: "18时" }, { value: "19", text: "19时" }, { value: "20", text: "20时" }, { value: "21", text: "21时" }, { value: "22", text: "22时" }, { value: "23", text: "23时" } ], min: [ { value: "0", text: "00分" }, { value: "10", text: "10分" }, { value: "20", text: "20分" }, { value: "30", text: "30分" }, { value: "40", text: "40分" }, { value: "50", text: "50分" } ] } }); ``` 2. 功能特点: HTML5+ 的 pickDate 可以简单的显示日期选择器,支持原生的日期格式,但是其样式无法自定义,功能比较简单。 MUI 的 dtpicker 支持日期、时间、日期时间三种类型的选择器,支持自定义样式和文字,支持自定义可选项,功能比较丰富。 3. 兼容性: HTML5+ 的 pickDate 兼容性较差,只有在支持 HTML5+ 的浏览器和 Webview 下才能正常显示。 MUI 的 dtpicker 兼容性较好,支持 iOS 和 Android 平台,同时也支持在 Webview 中使用。 综上所述,如果需要功能比较简单的日期选择器,可以使用 HTML5+ 的 pickDate,如果需要功能比较丰富的日期选择器,可以使用 MUI 的 dtpicker。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值