1. 程式人生 > >js獲取本月或指定月份的最後一天

js獲取本月或指定月份的最後一天

undefine day nth true || defined turn 獲取 null

自定義方法,用於獲取本月或指定月份的最後一天,如果不傳參數,就是當前月:

function getMonthFinalDay(year,month){
	var day=‘‘;
	if(year==null || year==undefined || year==‘‘){
		year = new Date().getFullYear();
	}
	if(month==null ||  month==undefined || month==‘‘){
		month = new Date().getMonth()+1;
	}
	day = new Date(new Date(year,month).setDate(0)).getDate();
	return year+"-"+month+"-"+day;
	
}

 調用方式:

getMonthFinalDay();//2017-10-31
getMonthFinalDay(2016);//2016-10-31
getMonthFinalDay(2017,9);//2017-9-30

  

js獲取本月或指定月份的最後一天