js 利用Date对象得到某个月份

function getMonth(addMonth) {
	var date = new Date();
	var m = date.getMonth() + 1;
	var y = date.getFullYear();
	if (m + addMonth <= 0) {
		yearCount =  Math.floor((0 - (m + addMonth)) / 12) + 1;
		y = y - yearCount;
		m = m + 12 * yearCount + addMonth;

	} else if (m + addMonth > 12){
			yearCount =  Math.floor((m + addMonth) / 12);
			y = y + yearCount;
			m = m - 12 * yearCount + addMonth;
	}

	m= m > 9 ? m:'0'+m;
	return y + "-" + m;
}
var res = getMonth(-1)
console.log(res)
上一篇:SQLServer 日期函数大全(转载)


下一篇:JS中根据身份证号获取年龄、出生日期