//当前时间加1个自然月
$scope.now = new Date()
var fromDate = $filter('date')($scope.now, 'yyyy-MM-dd')
getMonth(1, "-", $scope.now.getDate(),$scope.now)
//计算月份
function getMonth(num, format,day,date) {
date.setMonth(date.getMonth() + (num*1), 1);
console.log(date)
//读取日期自动会减一,所以要加一
var mo = date.getMonth() + 1;
//小月
if (mo == 4 || mo == 6 || mo == 9 || mo == 11) {
if (day > 30) {
day = 30
}
}
//2月
else if (mo == 2) {
if (isLeapYear(date.getFullYear())) {
if (day > 29) {
day = 29
}
}else if (day > 28) {
day = 28
}
}
//大月
else {
if (day > 31) {
day = 31
}
}
retureValue = date.format('yyyy' + format + 'MM' + format + day);
return retureValue;
}
//JS判断闰年代码
function isLeapYear(Year) {
if (((Year % 4) == 0) && ((Year % 100) != 0) || ((Year % 400) == 0)) {
return (true);
} else { return (false); }
}
//日期格式化
Date.prototype.format = function (format) {
var o = {
"M+": this.getMonth() + 1, // month
"d+": this.getDate(), // day
"h+": this.getHours(), // hour
"m+": this.getMinutes(), // minute
"s+": this.getSeconds(), // second
"q+": Math.floor((this.getMonth() + 3) / 3), // quarter
"S": this.getMilliseconds()
// millisecond
}
if (/(y+)/.test(format))
format = format.replace(RegExp.$1, (this.getFullYear() + "").substr(4 - RegExp.$1.length));
for (var k in o)
if (new RegExp("(" + k + ")").test(format))
format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length));
return format;
}
转:https://www.cnblogs.com/linyijia/p/6118835.html