1.Date()
1.1 Date()没有参数
返回当前的日期时间
1.2 Date(iyear,imonth,0)
返回本月天数
var d = Date(iyear,imonth,0) //0系统会自动当初本月的最后一天(即返回当月最后一天的日期)
d.getDate(); //返回月份的某一天,此处为本月最后一天,即本月天数
例:
var d = new Date(2016,2,0);
console.log(d.getDate());
例:级联时间
<div class="select">
<span style="white-space:pre"> </span><select name="year">
<span style="white-space:pre"> </span><option value="2015">2015</option>
<span style="white-space:pre"> </span></select>
<span style="white-space:pre"> </span><select name="month">
<span style="white-space:pre"> </span><option value="1">1</option>
<span style="white-space:pre"> </span></select>
<span style="white-space:pre"> </span><select name="day">
<span style="white-space:pre"> </span><option value="1">1</option>
<span style="white-space:pre"> </span></select>
</div>
<script>
var create_option = function(max,min){
var option = '';
if(max>min)
{
for(var i=min;i<=max;i++)
{
option+='<option value="'+i+'">'+i+'</option>';
}
}else{
for(var i=min;i>=max;i--)
{
option+='<option value="'+i+'">'+i+'</option>';
}
}
return option;
};
var set_date = function(y,m,d){
var Y = new Date().getFullYear();
$('select[name=year]').html(create_option(Y-100,Y));
if(y) $('select[name=year]').val(y);
$('select[name=month]').html(create_option(12,1));
if(m) $('select[name=month]').val(m);
$('select[name=day]').html(create_option(31,1));
if(d) $('select[name=day]').val(d);
};
var change_day = function(){
var year = $('select[name=year]').val();
var month = $('select[name=month]').val();
var day = $('select[name=day]').val();
var d = new Date(year,month,0);
$('select[name=day]').html(create_option(d.getDate(),1));
$('select[name=day]').val(day);
var tday = $('select[name=day]').val();
if(!tday) $('select[name=day]').val(1);
};
$(function(){
<span style="white-space:pre"> </span>set_date();
<span style="white-space:pre"> </span>$('body').on('change','select[name=year],select[name=month]',function(){
change_day();
});
})
</script>