Vue-js implements date addition and subtraction, year, month, day, and unit conversion.

1. Click to add one to the year

2. Click the year minus one

3. Click to add one month

4. Click the month minus one

5. Click the number of days plus one

6. Click the number of days minus one

7. The function of the checkMonth function is to add '0' in front of the single-digit month, for example: '7' becomes '07'

8. Time conversion function.

    1. Conversion date function (xx year xx month xx day xx hour xx minute xx second).

    2. Obtain the difference of XX days, xx hours, and xx minutes according to the time period

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

 1. Click to add one to the year

 // 点击年份加一
    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. Click the year minus one

 // 点击年份减一
    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. Click to add one month

 // 点击月份加一
    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. Click the month minus one

 // 点击月份减一
    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. Click the number of days plus one

//点击天数加一
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. Click the number of days minus one

//点击天数减一
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. The function of the checkMonth function is to add '0' in front of the single-digit month, for example: '7' becomes '07'

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

Conversion date function (xx year xx month xx day xx hour xx minute xx second).

function filterTime(time) {
    var date = new Date(time);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? "0" + m : m;
    var d = date.getDate();
    d = d < 10 ? "0" + d : d;
    var h = date.getHours();
    h = h < 10 ? "0" + h : h;
    var minute = date.getMinutes();
    minute = minute < 10 ? "0" + minute : minute;
    var s = date.getSeconds();
    s = s < 10 ? "0" + s : s;
    return y + "年" + m + "月" + d + "日" + " " + h + ":" + minute;
}

According to the time period, get the difference of XX days, xx hours, and xx minutes

// 根据时间段获取相差XX天xx小时xx分钟
function formatDuring(millisecond) {
    var days = parseInt(millisecond / (1000 * 60 * 60 * 24));
    var hours = parseInt(
        (millisecond % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)
    );
    var minutes = parseInt((millisecond % (1000 * 60 * 60)) / (1000 * 60));
    var seconds = (millisecond % (1000 * 60)) / 1000;
    return days + " 天 " + hours + " 小时 " + minutes + " 分钟 ";
}

Reference document: [Application] How to use JS to add and subtract dates by month_Dora_5537's blog-CSDN blog_js month plus one

If it is useful, please like it, follow it and add to favorites (。・ω・。)ノ♡

Guess you like

Origin blog.csdn.net/CCKing7/article/details/122105587