vue-js实现日期加减,年月日,及单位换算。

1.点击年份加一

2.点击年份减一

3.点击月份加一

4.点击月份减一

5.点击天数加一

6.点击天数减一

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

8.时间转化函数。

    1.转化日期函数(xx年xx月xx日xx时xx分xx秒)。

    2.根据时间段获取相差XX天xx小时xx分钟

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;
    },

转化日期函数(xx年xx月xx日xx时xx分xx秒)。

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;
}

根据时间段获取相差XX天xx小时xx分钟

// 根据时间段获取相差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 + " 分钟 ";
}

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

有用的话请点赞,关注加收藏哦(。・ω・。)ノ♡

猜你喜欢

转载自blog.csdn.net/CCKing7/article/details/122105587