日期从周五到周四为一周,本周不可选;周一开始为一周,选周日后在周日的时候,发现选择上一周的周一,却变成了本周一等问题

目录

一、引用day.js


1 、在终端输入yarn add dayjs

2 、在 main.js里面引用和注册组件:

import dayjs from 'dayjs'; //引用

Vue.prototype.dayjs = dayjs; //注册

3.在页面里面用,这里是比如要获取本周的上一周的周一:

mounted() {

const dayjs = require('dayjs');// 获取当前日期

const lastMonday = dayjs().add(-1, 'week').startOf('week').add(1, 'day')// 获取上一周的周一
this.dataValue = lastMonday.format('YYYY-MM-DD')

console.log(this.dataValue,'this.dataValue,刚进去页面的时候')  },

补充:在周日的时候,发现选择上一周的周一,却变成了本周一

因为 day.js默认是从周日开始一周的

解决:

const lastMonday = dayjs().add(-1, 'week').startOf('week').add(1, 'day')//这里获取上一周的时候,add(-1, 'week')这里用了-1

那我们就加个判断,如果是周二就-2

// 获取上一周的周一,因为 day.js是默认从周日开始一天的,所有要加判断 const weekday = dayjs().day() const we = weekday === 0 ? 0 - 2 : 0 - 1//周日是 0,周一是 1依此类推 const lastMonday = dayjs().add(we, 'week').startOf('week').add(1, 'day')

三、现在又要变成周五为起始的一周。

周五到周四为一周

pickerOptionsOwn: {

firstDayOfWeek: 5,

disabledDate(value) {

let today = new Date() //获取今天日期,

let d = today.getDay() //计算今天是周几。周五 5,周四 4,周三 3,周二 2,周一 1,周日 0

let end = ' '

switch (d) {

case 0: end = 3 break

case 1: end = 4 break

case 2: end = 5 break

case 3: end = 6 break

case 4: end = 7 break

case 5: end = 1 break

case 6: end = 2 break } l

et endTime = today.setDate(today.getDate() - end) return value.getTime() > endTime //不可选本周 },

},

mounted() {

const dayjs = require('dayjs') // 获取上一周的周一,因为 day.js是默认从周日开始一天的,所有要加判断

const weekday = dayjs().day() console.log(weekday, 'weekday,今天是周几的下标,周五 5,周四 4,周三 3,周二 2,周一 1,周日 0,周六 6') // const we = 1 // 周五,周六 // const we = 0 // 周日 const we = weekday === 5 || weekday === 6 ? 1 : 0 const endFirst = -9 const endLast = -3 const lasFistDay = dayjs().add(we, 'week').startOf('week').add(endFirst, 'day') const lastEndDay = dayjs().add(we, 'week').startOf('week').add(endLast, 'day') this.dataValue = lasFistDay.format('YYYY-MM-DD') this.reportDateEnd = lastEndDay.format('YYYY-MM-DD') console.log(this.dataValue, 'this.dataValue,刚进去页面的时候') // 获取上日(昨天) const yesterday = dayjs().subtract(1, 'day').format('MM-DD') this.onSubmit() this.yesTodayRate = yesterday console.log('this.yesToday', this.yesTodayRate) // 获取今日 const today = dayjs().format('MM-DD') this.today = today },

​​​​​​​

3.默认选择周一

<el-date-picker v-model="dataValue" :type="defaultItem.type" :format="defaultItem.format" :placeholder="defaultItem.placeholder" :clearable="false" :picker-options="pickerOptionsOwn" @change="showDate" /> showDate() { if (this.dateType == 'week') { let startTime = this.dayjs(this.dataValue).subtract(1, "day").$d; this.dataValue = this.splitDate(startTime); } else { let MOthDate = this.dataValue this.dataValue = this.splitDate(MOthDate); // let startTime2 = this.dayjs(this.dataValue).subtract(1, "month").$d; // this.dataValue = this.splitDate(startTime2); console.log(this.dataValue,'this.dataValue,选择月的') } // this.end = this.splitDate(endTime); // console.log(this.start, this.end, 'start,end') }, splitDate(date) { return this.dayjs(date).format("YYYY-MM-DD"); },

4.本周的不可以选择

pickerOptionsOwn: { firstDayOfWeek:1,//日历从周一开始 disabledDate(value) { let today = new Date(); //获取今天日期, let d = today.getDay(); //计算今天是周几。如果是周天,d=7 console.log(d,'d') let endTime = today.setDate(today.getDate()- d);//d=6, return value.getTime() > endTime; //不可选本周 }, },

// 使用 day.js获取最近两小时的时间

const dayjs = require('dayjs');

// 获取当前时间

const now = dayjs();

// 获取最近两小时的开始时间

const twoHoursAgo = now.subtract(2, 'hour').startOf('hour');

// 获取最近两小时的结束时间

const twoHoursFromNow = now.endOf('hour');

// 打印结果

console.log(`最近两小时的开始时间: ${twoHoursAgo.format()}`);

console.log(`最近两小时的结束时间: ${twoHoursFromNow.format()}`);

获取当前时间的前两小时 mounted() { // 获取当天的最近两小时 const dayjs = require('dayjs'); // 获取当前时间 const now = dayjs(); console.log(now,'获取当前时间......') // 提取当前时间的分钟和秒 const currentHour = now.hour(); const currentMinutes = now.minute(); const currentSeconds = now.second(); console.log(currentHour,currentMinutes,currentSeconds,'currentMinutes,currentSeconds') // 获取前两小时的开始时间 // const twoHoursAgo = now.subtract(2, 'hour').startOf('hour'); const twoHoursAgo = dayjs().hour(currentHour-2).minute(currentMinutes).second(currentSeconds); // 获取最近两小时的结束时间 // const twoHoursFromNow = now.endOf('hour'); // 结束时间用当前时间 this.datavalue = [twoHoursAgo.format('YYYY-MM-DD HH:mm:ss'), twoHoursFromNow.format('YYYY-MM-DD HH:mm:ss')]; // 打印结果 console.log(`最近两小时的开始时间: ${twoHoursAgo.format('YYYY-MM-DD HH:mm:ss')}`); console.log(`最近两小时的结束时间: ${twoHoursFromNow.format('YYYY-MM-DD HH:mm:ss')}`); },

1.获取当前日期

var today = new Date();  

2.获取昨天的日期

const dayjs = require('dayjs');

const yesterday = dayjs().subtract(1, 'day').format('YYYY-MM-DD');

let dateTimes = [ { id: 1, name: '本周', start_time: dayjs().startOf('week').add(1, 'day').format('YYYY-MM-DD'), end_time: dayjs().endOf('week').add(1, 'day').format('YYYY-MM-DD'), }, { id: 2, name: '上周', start_time: dayjs().add(-1, 'week').startOf('week').add(1, 'day').format('YYYY-MM-DD'), end_time: dayjs().add(-1, 'week').endOf('week').add(1, 'day').format('YYYY-MM-DD'), }, { id: 3, name: '本月', start_time: dayjs().startOf('month').format('YYYY-MM-DD') , end_time: dayjs().endOf('month').format('YYYY-MM-DD'), }, { id: 4, name: '上月', start_time: dayjs().add(-1, 'month').startOf('month').format('YYYY-MM-DD') , end_time: dayjs().add(-1, 'month').endOf('month').format('YYYY-MM-DD'), }, ] let curMonth = dayjs().month() + 1 if (curMonth < 3) { dateTimes.push( { id: 5, name: '本季度', start_time: dayjs().month(0).format('YYYY-MM-DD'), end_time: dayjs().month(2).endOf('month').format('YYYY-MM-DD'), }, { id: 6, name: '上季度', start_time: dayjs().add(-1, 'year').month(9).format('YYYY-MM-DD'), end_time: dayjs().add(-1, 'year').month(11).endOf('month').format('YYYY-MM-DD'), }, ) } else if (curMonth < 6) { dateTimes.push( { id: 5, name: '本季度', start_time: dayjs().month(3).format('YYYY-MM-DD'), end_time: dayjs().month(5).endOf('month').format('YYYY-MM-DD'), }, { id: 6, name: '上季度', start_time: dayjs().month(0).format('YYYY-MM-DD'), end_time: dayjs().month(2).endOf('month').format('YYYY-MM-DD') , }, ) } else if (curMonth < 9) { dateTimes.push( { id: 5, name: '本季度', start_time: dayjs().month(6).format('YYYY-MM-DD'), end_time: dayjs().month(8).endOf('month').format('YYYY-MM-DD'), }, { id: 6, name: '上季度', start_time: dayjs().month(3).format('YYYY-MM-DD'), end_time: dayjs().month(5).endOf('month').format('YYYY-MM-DD'), }, ) } else if (curMonth < 12) { dateTimes.push( { id: 5, name: '本季度', start_time: dayjs().month(9).format('YYYY-MM-DD'), end_time: dayjs().month(11).endOf('month').format('YYYY-MM-DD'), }, { id: 6, name: '上季度', start_time: dayjs().month(6).format('YYYY-MM-DD'), end_time: dayjs().month(8).endOf('month').format('YYYY-MM-DD'), }, ) }

```

  • 20
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值