js左右切换日期

废话不多说,直接上代码 

 

html代码

<img
							class="video_list_title_btn"
							src="../../assets/pc/course/left_2.png"
							@click="reducedate()"
							alt=""
						/>
						{{ time }}
						<img
							class="video_list_title_btn"
							src="../../assets/pc/course/right_2.png"
							@click="adddate()"
							alt=""
						/>

js代码 


	data() {
		return {
            time:''
		}
	},
	created() {
		var now = new Date()
		thi.time = this.formatDate(now.getFullYear(), now.getMonth() + 1, 1)
	},
	methods: {
		adddate() {
			//向右跳转时间(加时间)的按钮
			var s = this.time
			var arr = s.split('-') //将获取的数组按“/”拆分成字符串数组
			var year = parseInt(arr[0]) //开分字符串数组的第一个地址的内容是年份
			var mouth = parseInt(arr[1]) //开分字符串数组的第二个地址的内容是月份
			var date = parseInt(arr[arr.length - 1]) //开分字符串数组的第三个地址的内容是日期
			if (date == 28) {
				//当日期为28号时 只判断是否是2月
				switch (mouth) {
					case 2:
						if (
							(year % 4 == 0 && year % 100 != 0) ||
							year % 400 == 0
						) {
							date = date + 1
						} //如果是闰年2月 日期就加一
						else {
							date = 1
							mouth = mouth + 1
						} //不是闰年2月 日期就变为1 月份加一
						break
					default:
						date = date + 1
						break
				}
			} else if (date == 29) {
				//当日期为29号是 也是判断是否是2月
				switch (mouth) {
					case 2:
						date = 1
						mouth = mouth + 1
						break
					default:
						date = date + 1
						break
				} //当29号出现必定是闰年 日期变为1 月份加一
			} else if (date == 30) {
				//当日期为30 时
				switch (mouth) {
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
					case 12:
						date = date + 1
						break //这些月份的时候一个月有31天 到30的时候再加一
					case 4:
					case 6:
					case 9:
					case 11:
						date = 1
						mouth = mouth + 1
						break //这些月份的时候一个月有30天 到30的时候 日期变为1 月份加一
				}
			} else if (date == 31) {
				switch (mouth) {
					case 1:
					case 3:
					case 5:
					case 7:
					case 8:
					case 10:
						date = 1
						mouth = mouth + 1
						break //这些月份的时候一个月有31天 到31的时候 日期为1月份加一
					case 12:
						date = 1
						mouth = 1
						year = year + 1
						break //十二月 的 31 号 日期变为一 月份变为一 年份加一
				}
			} else {
				date += 1
			}
			this.time = this.formatDate(year, mouth, date)
		},
		reducedate() {
			//向左跳转时间(减时间)的按钮
			var s = this.time
			var arr = s.split('-') //将获取的数组按“/”拆分成字符串数组
			var year = parseInt(arr[0]) //开分字符串数组的第一个地址的内容是年份
			var mouth = parseInt(arr[1]) //开分字符串数组的第二个地址的内容是月份
			var date = parseInt(arr[arr.length - 1]) //开分字符串数组的第三个地址的内容是日期
			if (date == 1) {
				//当日期为1时,再剪就会改变月份,甚至年份
				switch (mouth) {
					case 1:
						date = 31
						mouth = 12
						year = year - 1
						break //一月一日 再剪一天 年份减一 月份为12 日期为31
					case 2:
					case 4:
					case 6:
					case 8:
					case 9:
					case 11:
						date = 31
						mouth = mouth - 1
						break //这些月一日 再剪一天 月份减一 日期为31
					case 3:
						if (
							(year % 4 == 0 && year % 100 != 0) ||
							year % 400 == 0
						) {
							date = 29
							mouth = mouth - 1
						} else {
							date = 28
							mouth = mouth - 1
						}
						break //三月一日 再剪一天 月份减一 日期为根据是否是闰年来判断 日期
					case 5:
					case 7:
					case 10:
						date = 30
						mouth = mouth - 1
						break //这些月一日 再剪一天 月份减一 日期为30
				}
			} else {
				date = date - 1
			}
			this.time = this.formatDate(year, mouth, date) //拼接字符串插入到标签中
		},
		formatDate: function(year, month, day) {
			var y = year
			var m = month
			if (m < 10) m = '0' + m
			var d = day
			if (d < 10) d = '0' + d
			return y + '-' + m + '-' + d
		},
    }

这样就齐活啦! 

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值