<div class="input-group date form_date" data-date="" data-date-format="" data-link-field="dtp_input2"
data-link-format="yyyy-mm-dd">
<input id="starTime" name="dueTime" class="form-control" size="16" type="text" value="" readonly>
<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
<div class="input-group date form_date" data-date="" data-date-format="" data-link-field="dtp_input2"
data-link-format="yyyy-mm-dd">
<input id="endTime" name="dueTime" class="form-control" size="16" type="text" value="" readonly>
<span class="input-group-addon"><span class="glyphicon glyphicon-remove"></span></span>
<span class="input-group-addon"><span class="glyphicon glyphicon-calendar"></span></span>
</div>
<button class="btn btn-default" id="searchLogin">查 询</button>
<input id="userIdQ" class="form-control" type="hidden"/>
</div>
复制代码
将项目拷贝后发现时间组件无法正常点击,需要添加下面一段文字。
$(".form_date").datetimepicker({ format: "yyyy-mm-dd", autoclose: true, todayBtn: true, todayHighlight: true, showMeridian: true, pickerPosition: "bottom-left", language: 'zh-CN',//中文,需要引用zh-CN.js包 startView: 2,//月视图 minView: 2//日期时间选择器所能够提供的最精确的时间选择视图 });
下面是封装的时间格式: //获取当前时间,格式YYYY-MM-DD function getNowFormatDate() { var date = new Date(); var seperator1 = "-"; var year = date.getFullYear(); var month = date.getMonth() + 1; var strDate = date.getDate(); if (month >= 1 && month <= 9) { month = "0" + month; } if (strDate >= 0 && strDate <= 9) { strDate = "0" + strDate; } var currentdate = year + seperator1 + month + seperator1 + strDate; return currentdate; }
将日期转换为纯数字进行比较方法;
starTime.replace(/\D/g,'');
要求日期和当前日期对比
//查询日期 $("#searchLogin").on('click', function () {
var value = $("#userIdQ").val();
var starTime = $("#starTime").val();
var endTime = $("#endTime").val();
var newTimeN = getNowFormatDate().replace(/\D/g,'')
var starTimeN = starTime.replace(/\D/g,'');
var endTimeN = endTime.replace(/\D/g,'')
if (starTimeN > newTimeN ) {
hint("开始日期不能大于当前日期")
}else if (endTimeN > newTimeN) {
hint("结束日期不能大于当前日期")
}else if (starTime.length == 0 && endTime.length == 0) {
return;
} else {
if (starTime.length == 0) {
hint("请输入开始日期!");
return;
}
if (endTime.length == 0) {
hint("请输入结束日期!");
return;
}
if (starTime > endTime) {
hint('请选择正确的起始日期!');
return;
}
var star = starTime.substring(0, 4);
var end = endTime.substring(0, 4);
if (star != end) {
hint("请选择同一年份下的日期!");
return;
}
}
复制代码