moment gets the first and last day of the current year, month, and week

moment gets the first and last day of the current year, month, and week
insert image description here

<u-icon class="icon-font" name="arrow-left" color="#ccc" size="15"
 									@click="switch_month_week('prev',true)"></u-icon>
 		<view class="title">{
   
   {datatime[0]}}-{
   
   {datatime[1]}}</view>
 								<u-icon class="icon-font" name="arrow-right" color="#ccc" size="15"
 									@click="switch_month_week('next',true)"></u-icon>
getdatatime(i, type) {
    
    
				// i:用户上一个月下一个也,type:获取的类型,年月周
 				if (type == "week") {
    
    
 					let weekOfDay = parseInt(moment().format('E')); //计算今天是这周第几天
 					let last_monday = moment().subtract(weekOfDay + 7 * i - 1, 'days').format('YYYY/MM/DD'); //周一日期
 					let last_sunday = moment().subtract(weekOfDay + 7 * (i - 1), 'days').format('YYYY/MM/DD'); //周日日期
 					return [last_monday, last_sunday]
 				} else if (type == "month") {
    
    
 					let startEndTime1 = moment().startOf("month").subtract(i, 'months').format('YYYY/MM/DD')
 					let startEndTime2 = moment().endOf("month").subtract(i, 'months').format('YYYY/MM/DD')
 					
 					return [startEndTime1, startEndTime2]

 				} else if (type == "year") {
    
     
 					let startDate = moment().startOf('year').format('YYYY/MM/DD')
 					let endDate = moment().endOf('year').format('YYYY/MM/DD')
 					return [startDate, endDate]
 				}

 			},


			// nowTime:默认是0,当前年/月/周
 			switch_month_week(type = 'next') {
    
    
 				if (type == 'prev') {
    
    
 					this.nowTime = this.nowTime + 1
 				} else if (type == 'next') {
    
    
 					this.nowTime = this.nowTime - 1
 				}
				// 调用函数获取到年月周欧
 				this.datatime = this.getdatatime(this.nowTime, this.timetype)  
 			},

Guess you like

Origin blog.csdn.net/weixin_44590591/article/details/129707952
Recommended