时间戳+指定的月份

212 阅读1分钟
<!DOCTYPE html>
<html lang="zh">
	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1.0">
		<meta http-equiv="X-UA-Compatible" content="ie=edge">
		<title></title>
		<script src="https://cdn.jsdelivr.net/npm/vue@2.6.14"></script>

	</head>
	<body>
		<div id="app"></div>
	</body>
	<script type="text/javascript">
		new Vue({
			el: '#app',
			data: {
			},
			created() {
				var timestamp1 = Date.parse(new Date())
				console.log('当前时间',this.getYMDHMS(timestamp1));
				var str = '2018-01-01 00:00:00';
				var str1 = this.getYMDHMS(timestamp1).replace(/-/g,'/'); 
				var date = new Date(Date.parse(str1))
				Date.prototype.addMonth = function (addMonth) {
				        var y = this.getFullYear();
				        var m = this.getMonth();
				        var nextY = y;
				        var nextM = m;
						
						 //如果当前月+增加的月>11 这里之所以用11是因为 js的月份从0开始
						let totalMonth=(m + addMonth);
						if(totalMonth<=12){
							 nextM = this.getMonth() + addMonth
						}else{
							let yearNum=parseInt(totalMonth/11);
							console.log('余'+yearNum);
							nextY = y + yearNum;
							nextM = parseInt(totalMonth) - (yearNum*12);
						}  
				        var daysInNextMonth = Date.daysInMonth(nextY, nextM);
				        var day = this.getDate();
				        if (day > daysInNextMonth) {
				            day = daysInNextMonth;
				        }
				        return new Date(nextY, nextM, day);
				    };
				
				    //计算当前月最大天数
				    Date.daysInMonth = function (year, month) {
				        if (month == 1) {
				            if (year % 4 == 0 && year % 100 != 0)
				                return 29;
				            else
				                return 28;
				        } else if ((month <= 6 && month % 2 == 0) || (month > 6 && month % 2 == 1))
				            return 31;
				        else
				            return 30;
				    };
				var nowDate = date.addMonth(23);   //date格式的时间类型
				console.log(str1);
				
				
				Date.prototype.format = function (format) {
				        var date = {
				            "M+": this.getMonth() + 1,
				            "d+": this.getDate(),
				            "h+": this.getHours(),
				            "m+": this.getMinutes(),
				            "s+": this.getSeconds(),
				            "q+": Math.floor((this.getMonth() + 3) / 3),
				            "S+": this.getMilliseconds()
				        };
				        if (/(y+)/i.test(format)) {
				            format = format.replace(RegExp.$1, (this.getFullYear() + '').substr(4 - RegExp.$1.length));
				        }
				        for (var k in date) {
				            if (new RegExp("(" + k + ")").test(format)) {
				                format = format.replace(RegExp.$1, RegExp.$1.length == 1
				                    ? date[k] : ("00" + date[k]).substr(("" + date[k]).length));
				            }
				        }
				        return format;
				    };
					var nowStr = nowDate.format('yyyy-MM-dd hh:mm:ss');   //指定字符串格式的时间类型
					console.log(nowStr);
			},
			methods:{
				  //时间戳转年月日时分秒
				    getYMDHMS (timestamp) {
				        let time = new Date(timestamp)
				              let year = time.getFullYear()
				              let month = time.getMonth() + 1
				              let date = time.getDate()
				              let hours = time.getHours()
				              let minute = time.getMinutes()
				              let second = time.getSeconds()
				        
				              if (month < 10) { month = '0' + month }
				              if (date < 10) { date = '0' + date }
				              if (hours < 10) { hours = '0' + hours }
				              if (minute < 10) { minute = '0' + minute }
				              if (second < 10) { second = '0' + second }
				              return year + '-' + month + '-' + date + ' ' + hours + ':' + minute + ':' + second
				    }
			}
		})
	</script>
	<style type="text/css">
		#app {
			font-size: 120rem;
			line-height: 100rem;
		}
	</style>
</html>