近期在使用Datepicker插件选择日期时,遇到一种需求:可选日期范围为当前年份第一天至当前日期或者当前月份第一天至当前日期,查了一些资料,实现了可选范围为当前年份第一天至当前日期和当前月份第一天至当前日期的功能。
代码如下:
<link href="${staticPath}/static/plugins/bootstrap/css/datepicker3.css" rel="stylesheet" type="text/css" />
<input id="getTime" name="getTime" type="text" placeholder="获得时间" readonly />
1.实现当前月份第一天至当前日期的范围选择:
function getYear()
{
var y = new Date().getFullYear();
return y;
}
function getMonth()
{
var m = new Date().getMonth();
return m;
}
$("#getTime").datepicker({
showOtherMonths : false,
selectOtherMonths : false,
startDate:'getYear().getMonth().1', //开始时间,在这时间之前都不可选
endDate:'+1',//结束时间,在这时间之后都不可选
autoclose : true,
language : "cn",
format : "yyyy-mm-dd"
});
2.实现当前年份第一天至当前日期的范围选择:
function getDateTimeStr()
{
var s = new Date().getFullYear();
return s;
}
$("#getTime").datepicker({
showOtherMonths : false,
selectOtherMonths : false,
startDate:'getDateTimeStr().1.1', //开始时间,在这时间之前都不可选
endDate:'+1',//结束时间,在这时间之后都不可选
autoclose : true,
language : "cn",
format : "yyyy-mm-dd"
});
< (小白一枚,请多指教) />
![奋斗](http://static.blog.csdn.net/xheditor/xheditor_emot/default/struggle.gif)