日期 Date


| 日期

获取某年某月多少天:new Date(new Date(2017,12,1).getTime()-864e5).getDate(); 转换日期格式:new Date().toLocaleDateString().replace(/\//g,”-“); 日期字符串排序:new Date(a).getTime();

字符串时间日期比较:new Date(”2017-02-02 13:23:23”).getTime()

—– 方法封装———-

| 日期格式转换 2018-02-02

Vue.prototype.$formatDate = time => {
  if (time && time != '') {
      let dataTime = new Date(time).toLocaleDateString().replace(/\//g, "-");
      dataTime = dataTime.split('-').map(item => {
          if (item > 0 && item < 10) {
              return '0' + item;
          } else {
              return item;
          }
      }).join('-');
      return dataTime;
  } else {
      return ''
  }
}
// 返回格式: 2012/02/18
formatDate(str) {
  const date = new Date(str);
  const year = date.getFullYear();
  const montn = ("0" + (date.getMonth() + 1)).slice(-2);
  const day = ("0" + date.getDate()).slice(-2);
  const h = ("0" + date.getHours()).slice(-2);
  const m = ("0" + date.getMinutes()).slice(-2);
  return `${year}/${montn}/${day}`;
}

| 获取某年某月多少天

getDaysInMonth(year, month) {
    var date = new Date(year, month, 1);
    return new Date(date.getTime() - 864e5).getDate();
}

| 获取当前周第一天和最后一天

getWeekStartAndEnd(date) {
    var date = new Date(date);
    //周几
    var week = date.getDay();
    var minusDay = week != 0 ? week - 1 : 6;
    //一天的毫秒数
    var millisecond = 1000 * 60 * 60 * 24;
    //获得当前周的第一天
    var currentWeekFirstDay = new Date(date.getTime() - (millisecond * minusDay));
    // //获得当前周的最后一天
    var currentWeekLastDay = new Date(currentWeekFirstDay.getTime() + (millisecond * 6));
    this.taskStartTime = this.changeTime(currentWeekFirstDay);
    this.taskEndTime = this.changeTime(currentWeekLastDay);
}