javascript获取当月第一天和最后一天,有两种方式:
1、javascript方式
var date_ = new Date(); var year = date_.getYear(); var month = date_.getMonth() + 1; var firstdate = year + '-' + month + '-01' var month_first = formatDate(parseDate(firstdate),"yyyy-MM-dd")+" 00:00:00"; var day = new Date(year,month,0); var lastdate = year + '-' + month + '-' + day.getDate(); var month_last = formatDate(parseDate(lastdate),"yyyy-MM-dd")+" 24:00:00"; //alert(firstdate);alert(month_first); //alert(lastdate);alert(month_last);
2、利用 xdate.js 库
var firstDate = new Date(); firstDate.setDate(1); //第一天 var endDate = new Date(firstDate); endDate.setMonth(firstDate.getMonth()+1); endDate.setDate(0); alert("第一天:"+new XDate(firstDate).toString('yyyy-MM-dd')); alert("最后一天:"+new XDate(endDate).toString('yyyy-MM-dd'));