学无先后,达者为师

网站首页 Vue 正文

js实现日期加减,年月日 -vue

作者:超超有点阔爱 更新时间: 2022-01-01 Vue

1.点击年份加一

2.点击年份减一

3.点击月份加一

4.点击月份减一

5.点击天数加一

6.点击天数减一

7.checkMonth 函数的作用是将单位数的月份前面加 ‘0’,比如:‘7’ 变成 ‘07’

data() {
    return {
      yearMonthValue: "2021-12-23",
    };
  },

 1.点击年份加一

 // 点击年份加一
    YearAdd() {
      let currentDate = this.yearMonthValue;
      currentDate = new Date(currentDate);
      let nextDate = currentDate.setYear(currentDate.getFullYear() + 1); // 输出日期格式为毫秒形式1556668800000
      nextDate = new Date(nextDate);
      console.log(nextDate);
      this.yearMonthValue = nextDate;
    },

 2.点击年份减一

 // 点击年份减一
    YearReduce() {
      let currentDate = this.yearMonthValue;
      currentDate = new Date(currentDate);
      let lastDate = currentDate.setYear(currentDate.getFullYear() - 1); // 输出日期格式为毫秒形式1556668800000
      lastDate = new Date(lastDate);
      console.log(lastDate);
      this.yearMonthValue = lastDate;
    },

3.点击月份加一

 // 点击月份加一
    MonthAdd() {
      let currentDate = this.yearMonthValue;
      currentDate = new Date(currentDate);
      let nextDate = currentDate.setMonth(currentDate.getMonth() + 1); // 输出日期格式为毫秒形式1556668800000
      nextDate = new Date(nextDate);
      let nextYear = nextDate.getFullYear();
      let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
      nextDate = nextYear + "-" + nextMonth; // "2019-05"
      console.log(nextDate);
      this.yearMonthValue = nextDate;
    },

4.点击月份减一

 // 点击月份减一
    MonthReduce() {
      let currentDate = this.yearMonthValue;
      currentDate = new Date(currentDate);
      let lastDate = currentDate.setMonth(currentDate.getMonth() - 1); // 输出日期格式为毫秒形式1551398400000
      lastDate = new Date(lastDate);
      let lastYear = lastDate.getFullYear();
      let lastMonth = this.checkMonth(lastDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
      lastDate = lastYear + "-" + lastMonth; // "2019-03"
      console.log(lastDate);
      this.yearMonthValue = lastDate;
    },

5.点击天数加一

//点击天数加一
DayAdd(){
      let currentDate = this.yearMonthValue;
      currentDate = new Date(currentDate);
      let nextDate = currentDate.setMonth(currentDate.getMonth()); // 输出日期格式为毫秒形式1556668800000
      nextDate = new Date(nextDate);
      let nextYear = nextDate.getFullYear();
      let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
      let nextDay = currentDate.setDate(currentDate.getDate() + 1); // 输出日期格式为毫秒形式1556668800000
      nextDay = new Date(nextDay);
      let nextday = nextDay.getDate()
      nextDate = nextYear + "-" +nextMonth+"-"+nextday; // "2019-05"
      console.log(nextDate);
       this.yearMonthValue = nextDate;
    },

6.点击天数减一

//点击天数减一
DayReduce(){
      let currentDate = this.yearMonthValue;
      currentDate = new Date(currentDate);
      let nextDate = currentDate.setMonth(currentDate.getMonth()); // 输出日期格式为毫秒形式1556668800000
      nextDate = new Date(nextDate);
      let nextYear = nextDate.getFullYear();
      let nextMonth = this.checkMonth(nextDate.getMonth() + 1); // 因日期中的月份表示为0-11,所以要显示正确的月份,需要 + 1
      let nextDay = currentDate.setDate(currentDate.getDate() - 1); // 输出日期格式为毫秒形式1556668800000
      nextDay = new Date(nextDay);
      let nextday = nextDay.getDate()
      nextDate = nextYear + "-" +nextMonth+"-"+nextday; // "2019-05"
      console.log(nextDate);
       this.yearMonthValue = nextDate;
    },

7.checkMonth 函数的作用是将单位数的月份前面加 ‘0’,比如:‘7’ 变成 ‘07’

 //checkMonth 函数的作用是将单位数的月份前面加 ‘0’,比如:‘7’ 变成 ‘07’
  checkMonth(i) {
      if (i < 10) {
        i = "0" + i;
      }
      return i;
    },

参考文档:【应用】如何使用JS实现日期按月份加减_Dora_5537的博客-CSDN博客_js月份加一

原文链接:https://blog.csdn.net/CCKing7/article/details/122105587

栏目分类
最近更新