vue中使用new Date()封装方法获取需要的某些时间格式xxxx-xx-xx

vue中,我们通过Vue.prototype封装方法供项目随时使用,具体见代码如下:

  1. 传入时间转换为 xxxx-xx-xx 的时间格式
    Vue.prototype.getCurrentDataStr = dateStr => {
      let date = new Date(dateStr);
      let y = date.getFullYear();
      let m = date.getMonth() + 1;
      m = m < 10 ? '0' + m : m;
      let d = date.getDate();
      d = d < 10 ? '0' + d : d;
      return y + '-' + m + '-' + d;
    };
  1. 获取传入时间的前一天的时间 xxxx-xx-xx
    Vue.prototype.getYesterdayDataStr = dateStr => {
      let time = new Date(dateStr - 24 * 60 * 60 * 1000);
      let y = time.getFullYear();
      let m = time.getMonth() + 1;
      m = m < 10 ? '0' + m : m;
      let d = time.getDate();
      d = d < 10 ? '0' + d : d;
      return y + '-' + m + '-' + d;
    };
  1. 传入时间转换为 星期x x月x日 的时间格式
    Vue.prototype.getTodayDataStr = dateStr => {
      let date = new Date(dateStr);
      let weeks = ['日', '一', '二', '三', '四', '五', '六'];
      let m = date.getMonth() + 1;
      m = m < 10 ? '0' + m : m;
      let d = date.getDate();
      d = d < 10 ? '0' + d : d;
      let weekIndex = date.getDay();
      weekIndex = weeks[weekIndex];
      return '星期' + weekIndex + ' ' + m + '月' + d + '日';
    };
  1. 获取传入时间所在 周的第一天 xxxx-xx-xx
    Vue.prototype.getThisWeekFirst = dateStr => {
      let today = new Date(dateStr).getTime();
      let weekIndex = new Date().getDay();
      let weekFirstDay = new Date(today - (weekIndex - 1) * 86400000);
      let y = weekFirstDay.getFullYear();
      let m = weekFirstDay.getMonth() + 1; //获取月份
      m = m < 10 ? '0' + m : m;
      let d = weekFirstDay.getDate();
      d = d < 10 ? '0' + d : d;
      return y + '-' + m + '-' + d;
    };
  1. 获取传入时间所在 周的最后一天 xxxx-xx-xx
    Vue.prototype.getThisWeekEnd = dateStr => {
      let today = new Date(dateStr).getTime();
      let weekIndex = new Date().getDay();
      let weekFirstDay = new Date(today - (weekIndex - 1) * 86400000).getTime();
      let WeekLastDay = new Date(weekFirstDay + 6 * 86400000);
      let y = WeekLastDay.getFullYear();
      let m = WeekLastDay.getMonth() + 1; //获取月份
      m = m < 10 ? '0' + m : m;
      let d = WeekLastDay.getDate();
      d = d < 10 ? '0' + d : d;
      return y + '-' + m + '-' + d;
    };
  1. 获取传入时间所在 月的第一天 xxxx-xx-xx
    Vue.prototype.getThisMonthFirst = dateStr => {
      let time = new Date(dateStr);
      let y = time.getFullYear();
      let m = time.getMonth() + 1;
      m = m < 10 ? '0' + m : m;
      return y + '-' + m + '-' + '01';
    };
  1. 获取传入时间的上月的第一天 xxxx-xx-xx
    Vue.prototype.getLastMonthStart = dateStr => {
      let time = new Date(dateStr);
      let y = time.getFullYear();
      let m = time.getMonth();
      m = m < 10 ? '0' + m : m;
      return y + '-' + m + '-' + '01';
    };
  1. 获取传入时间所在 月的最后一天 xxxx-xx-xx
    Vue.prototype.getThisMonthEnd = dateStr => {
      let date = new Date(dateStr); //当前日期
      let nextMonthDayOne = new Date(
        date.getFullYear(),
        date.getMonth() + 1,
        1
      ).getTime();
      let thisMonthDayEnd = new Date(nextMonthDayOne - 86400000);
      let y = thisMonthDayEnd.getFullYear(); //当前年
      let m = thisMonthDayEnd.getMonth() + 1; //当前月
      m = m < 10 ? '0' + m : m;
      let d = thisMonthDayEnd.getDate();
      d = d < 10 ? '0' + d : d;
      return y + '-' + m + '-' + d;
    };
  1. 获取传入时间的上一年的时间xxxx-xx-xx
    Vue.prototype.getLastYearToday = dateStr => {
      let time = new Date(dateStr);
      let y = time.getFullYear() - 1;
      let m = time.getMonth() + 1;
      m = m < 10 ? '0' + m : m;
      let d = time.getDate();
      d = d < 10 ? '0' + d : d;
      return y + '-' + m + '-' + d;
    };

使用方法为:

 const timeStamp = new Date().getTime();  // 任何想要转换的**时间戳格式**的时间
 this.**getThisMonthEnd**(timeStamp )   // getThisMonthEnd 为上述方法8

写在最后:
手动写法固然代码量颇多,但是能帮我们更好的整理思路。
如果想要一行代码就搞定需求或者传参,请移步此处 moment()获取以天、周、月、季度、年为单位的具体时间

你可能感兴趣的:(JavaScript,javascript)