Vue gets the current week date

method one

First of all, we confirm the date and day of the week by getting today's time. At this time, three situations will appear:

1. Current Monday;

2. Current Sunday;

3. It is currently any day from Tuesday to Friday;

That is to say, we clearly know today's date and today's day of the week, and get the time through new Date()

Two arrays are needed to store the name and date of each day. You can also create an array object [{week:'',time:''}] at the beginning, and don't say much about the code according to your personal preference.

const days = ref([''])// 存放一周日期
const week = ref();// 存放当天周几
const weeks = ['星期一','星期二','星期三','星期四','星期五','星期六','星期日']

const generateDateModel = () => {
// 当前时间
const myDate = new Date();
// 年份
const year = myDate.getFullYear();
// 月份
const month = myDate.getMonth()+1;
// 今天周几,number
week.value = myDate.getDay();

// 今天日期,number
const day = myDate.getDate();

// 存放今天到周日的天数
const futureArr = ref()

// 本周已过天数
const pastArr = ref()

// 把今天周几转换成string
week.value = weeks[week.value];

for(let i = 0;i < weeks.length;i+=1){
//判断今天是不是周二到周六中的一天
    if(week.value !== weeks[0] && week.value !== weeks[6]){
// 找到今天索引
      const index = weeks.indexOf(week.value); 
// 今天的日期
      days.value[index-1] = `${year}/${month}/${day}` 
// 今天到周日的天数
      const future = 7 - index 
// 本周已过天数
      const past = index - 1 
// 获取本周剩余日期
      for (let j = future; j > 0 ; j -= 1){ 
        days.value[index + j - 1] = `${year}/${month}/${day+j}`
      }
// 获取本周已过日期
      for (let j = 0; j < past; j += 1){ 
        days.value[j] = `${year}/${month}/${day-j-1}`
      }
// 如果今天是周一获取本周日期
    if(week.value === weeks[0]){ 
      for(let j = 1; j < 7; j += 1){
         days.value[i] = `${year}/${month}/${day+i}`
      }
    }
// 如果今天是周日过去七天日期
      if(week.value === weeks[6]){ 
        for(let j = 7; j > 0; j -= 1){
          days.value[i] = `${year}/${month}/${day-i}`
      }
    }
  }
}

generateDateModel();

The above code can realize the date of the seven days of the current week, but the code is too redundant, and there is another problem that there is no order, so it is not recommended

Method Two

When we know the current date, we can directly deduce Monday or Sunday, so we can directly subtract the corresponding number of days from today’s date to get Monday or Sunday and arrange them in order. The code is as follows:

const days = ref([''])
const week = ref();
const weeks = ['星期一','星期二','星期三','星期四','星期五','星期六','星期日']


const generateDateModel = () => {
  const myDate = new Date();
  const year = myDate.getFullYear();
  const month = myDate.getMonth() + 1;
  week.value = myDate.getDay() - 1;// 此方法查询出来的是当前周几的数字,比如周一就是1
  let day = myDate.getDate();
  week.value = weeks[week.value];
  if(week.value === weeks[0]){ // 如果今天是周一获取本周日期
    for(let j = 0; j < 7; j += 1){
       days.value[j] = `${year}/${month}/${day + j}`
    }
  }else{
    const index = weeks.indexOf(week.value); // 找到今天索引
     day -= index ; // 周一
     for(let j = 0; j < 7; j += 1){
       days.value[j] = `${year}/${month}/${day + j}`
    }
  }
}

generateDateModel();

This method can realize the time of the latest week. If the current time is the last or first day of this month, there will be problems.

Method 2 Improvement

First of all, it is correct to think in the back row based on Monday, but the simple addition and subtraction of values ​​will appear at the beginning of the month and the end of the month to a negative value or add to more than 31, so it needs to be converted into a timestamp first, and the 86400000 of the day is subtracted from the timestamp. First get today's index and add or subtract by index, the code is as follows

There is a better way you can recommend to me

const days = ref([''])
const week = ref();
const day = ref();
const weeks = ['星期一','星期二','星期三','星期四','星期五','星期六','星期日']

const generateDateModel = () => {
  const myDate = new Date();
  const year = myDate.getFullYear();
  const month = myDate.getMonth()+1;
  week.value = myDate.getDay()-1;
  day.value = myDate.getDate();
  week.value = week.value === -1 ? weeks[6] : weeks[week.value];
if(week.value === weeks[0]){ // 如果今天是周一获取本周日期
  days.value[0] = `${year}/${month}/${myDate.getDate()}`
  for(let j = 1; j < 7; j += 1){
    const oneDay = new Date(days.value[0]).getTime() + j * 86400000
    days.value[j] =  `${new Date(oneDay).getFullYear()}/${new Date(oneDay).getMonth() + 1}/${new Date(oneDay).getDate()}`
  }
}else{
  const index = weeks.indexOf(week.value); // 找到今天索引
  const Monday = Date.now() - index * 86400000
  days.value[0] = `${new Date(Monday).getFullYear()}/${new Date(Monday).getMonth() + 1}/${new Date(Monday).getDate()}`
  for (let i = 1; i < 7; i+=1) {
    const oneDay = Monday + i * 86400000
    days.value[i] =  `${new Date(oneDay).getFullYear()}/${new Date(oneDay).getMonth() + 1}/${new Date(oneDay).getDate()}`
  }
}
  
}

console.log(days.value)

generateDateModel();

The following is the final output:

 

Guess you like

Origin blog.csdn.net/weixin_51265675/article/details/127618890
Recommended