js获取月份第一天和最后一天
<script>
$(function (){
var date = new Date();
var year = date.getFullYear();
var month = date.getMonth() + 1;
if (month >= 1 && month <= 9) {
month = "0" + month;
}
//js 获取当前月份 第一天和最后一天
var currentdate1 = year+"-"+ month+"-"+ "01";
var monthEndDate = new Date(year, month, 0);
$("#startDate").val(currentdate1)
$("#endDate").val(ChangeDateFormat(monthEndDate.getTime(),"yyyy-MM-dd"))
})
</script>
由于其他同事已经自定义转换方法ChangeDateFormat(json格式后日期(毫秒数)转成日期格式),直接调用
附上自定义ChangeDateFormat方法
//将序列化成json格式后日期(毫秒数)转成日期格式
function ChangeDateFormat(cellval, type) {
if (cellval && cellval !='undefined') {
cellval+="";
var date = date;
if (cellval.indexOf("-") == 4)
date = new Date(cellval);
else if (cellval.indexOf("/Date(") == -1)
date = new Date(parseInt(cellval, 10));
else
date = new Date(parseInt(cellval.replace("/Date(", "").replace(")/", ""), 10));
var monthDate = date.getMonth() + 1 < 10 ? "0" + (date.getMonth() + 1) : date.getMonth() + 1;
var dayDate = date.getDate() < 10 ? "0" + date.getDate() : date.getDate();
var hoursDate = date.getHours() < 10 ? "0" + date.getHours() : date.getHours();
var mnuteDate = date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes();
var secondsDate = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
var strDate = "";
switch (type) {
case "yyyy-MM-dd": strDate = date.getFullYear() + "-" + monthDate + "-" + dayDate;
break;
case "yy.MM.dd": strDate = date.getFullYear().toString().substr(2,2) + "." + monthDate + "." + dayDate;
break;
case "yy-MM-dd": strDate = date.getFullYear().toString().substr(2, 2) + "-" + monthDate + "-" + dayDate;
break;
case "yyyy-MM-dd HH:mm": strDate = date.getFullYear() + "-" + monthDate + "-" + dayDate + " " + hoursDate + ":" + mnuteDate;
break;
case "MM-dd HH:mm": strDate = monthDate + "-" + dayDate + " " + hoursDate + ":" + mnuteDate;
break;
case "yyyy-MM": strDate = date.getFullYear() + "-" + monthDate;
break;
case "MM-dd": strDate = monthDate + "-" + dayDate;
break;
case "mm-dd": strDate = monthDate + "月" + dayDate + "日";//20190311
break;
case "MM-DD": strDate = dayDate + "-" + MonthStr(date.getMonth() + 1);//20190311
break;
case "MM-DD-YYYY": strDate = dayDate + "." + MonthStr(date.getMonth() + 1)+date.getFullYear();//20190315
break;
case "MM/dd": strDate = monthDate + "/" + dayDate;
break;
case "HH:mm:ss": strDate = hoursDate + ":" + mnuteDate + ":" + secondsDate;
break;
case "HH:mm": strDate = hoursDate + ":" + mnuteDate;
break;
case "HHmm": strDate = hoursDate+""+ mnuteDate;
break;
case "yyyyMMddHHmm": strDate = date.getFullYear() + "-" + monthDate + "-" + dayDate + " " + hoursDate + ":" + mnuteDate;
break;
default: strDate = date.getFullYear() + "-" + monthDate + "-" + dayDate + " " + hoursDate + ":" + mnuteDate + ":" + secondsDate;
}
return strDate;
}
else {
return "";
}
}
效果
js获取当前日期
<script>
$(function (){
var date = new Date();
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;
}
//js 获取当前月份 当前天数
var currentdate = year+"-"+ month+"-"+ strDate;
$("#startDate").val(currentdate)
$("#endDate").val(currentdate)
})
</script>
效果