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; }
根据年月获取当月第一天和最后一天
//根据年月获取当月第一天日期 function getStartDate(yearmonthstr){ if(/^\d{4}-\d{2}$/.test(yearmonthstr)){ //判断是否满足yyyy-mm条件 var year = /\d{4}/.exec(yearmonthstr)[0]; //获取年份 var month = /\d{2}$/.exec(yearmonthstr)[0]; //获取月份 var d = new Date(year, month, 0); var day = d.getDate(); //获取月的天数 return yearmonthstr+'-01'; } } //根据年月获取当月最后一天日期 function getEndDate(yearmonthstr){ if(/^\d{4}-\d{2}$/.test(yearmonthstr)){ //判断是否满足yyyy-mm条件 var year = /\d{4}/.exec(yearmonthstr)[0]; //获取年份 var month = /\d{2}$/.exec(yearmonthstr)[0]; //获取月份 var d = new Date(year, month, 0); var day = d.getDate(); //获取月的天数 return yearmonthstr+'-'+day; } }