记录1:动态得到当前一个星期的时间

里插入图片描述
获取到的时间就像上图中一样,特此为自己做个记录:

getTime: function() {
				this.tabBarList = []
				let weekarr = ['星期天', '星期一', '星期二', '星期三', '星期四', '星期五', '星期六'];
				for (var i = 0; i <= 6; i++) {
					const time = new Date(new Date().setDate((new Date().getDate() + i)));
					const month = `0${time.getMonth()}`.slice(-2); //月
					const strDate = `0${time.getDate()}`.slice(-2); //号
					const weeks = `${time.getDay()}`.slice(-2); //星期

					if (i == 0) {
						this.tabBarList.push({
							date: `${month}-${strDate}`,
							id: i,
							name: "今天"
						})
						// 把当前的月份日期存储到本地
						this.date = `${month}-${strDate}`;
					} else if (i == 1) {
						this.tabBarList.push({
							date: `${month}-${strDate}`,
							id: i ,
							name: "明天"
						})
					} else {
						this.tabBarList.push({
							date: `${month}-${strDate}`,
							id: i,
							name: weekarr[weeks]
						})
					}
				}
			},

其中:1. new Date() , setDate() 和 getDate()
new Date() 获取的是当前的日期
setDate() 设置天数,并且必须接收一个整数。,如:在这里插入图片描述
表示一个月中的一天的一个数值(1 ~ 31)
getDate() 返回的是本月份的几天

2.关于slice要注意:slice是截取的作用,通常里面有两个值,分别表示开始和结束的索引,但是当参数变成负值的时候,要先加上原本的长度才开始截取,比如这里的slice(-2)实际上就是slice(1).
更加详细的解释可以看看这个文章,作者在里面详细的阐述了关于slice以及substring,substr方法传入负值的时候的变化:https://blog.csdn.net/weixin_38382659/article/details/90267317

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值