封装常用处理时间的方法

分装成一个函数,并返回封装好的处理时间的方法集合:

var GetFormatDate = function (date) {
    if (Object.prototype.toString.call(date) == "[object String]" && date.length === 8) {
      date = new Date(date.substr(0, 4), parseInt(date.substr(4, 2) - 1), date.substr(6, 2));
    }
    if (Object.prototype.toString.call(date) == "[object String]" && date.length === 14) {
      date = new Date(date.substr(0, 4), parseInt(date.substr(4, 2) - 1), date.substr(6, 2), date.substr(8, 2), date.substr(10, 2), date.substr(12, 2));
    }
    if (Object.prototype.toString.call(date) != "[object Date]") {
      throw new Error("参数必须为时间格式Date/n或者格式为8或14的字符串。比如2020-10-27用20201027表示");
      return false;
    }
    const year = date.getFullYear(); // 年份
    const month = date.getMonth();   // 月份
    const currentMonth = date.getMonth() + 1;
    const days = date.getDate();     // 天
    const hours = date.getHours();   //小时
    const minutes = date.getMinutes(); //分钟
    const seconds = date.getSeconds(); //秒
    const milliseconds = date.getMilliseconds();//毫秒
    const week = date.getDay(); //星期
    const timeOfDay = 24 * 60 * 60 * 1000;
    return {
      //格式化日期
      getFormatDate: function (flag) {
        return flag === undefined ? (year + "-" + (currentMonth > 9 ? currentMonth : "0" + currentMonth) + "-" + (days > 9 ? days : "0" + days)) : (year + "年" + currentMonth + "月" + days + "日");
      },
      //格式化时间
      getFormatTime: function (flag) {
        return flag === undefined ? (this.getFormatDate() + " " + (hours > 9 ? hours : "0" + hours) + ":" + (minutes > 9 ? minutes : "0" + minutes) + ":" + (seconds > 9 ? seconds : "0" + seconds)) : (this.getFormatDate(flag) + " " + hours + "时" + minutes + "分" + seconds + "秒");
      },
      //获取当前星期
      getWeekUS: function (flag) {
        var weekObj = {};
        switch (week) {
          case 1:
            weekObj = {
              "US": "Monday",
              "CN": "星期一",
              "USab": "Mon"
            };
            break;
          case 2:
            weekObj = {
              "US": "Tuesday",
              "CN": "星期二",
              "USab": "Tue"
            };
            break;
          case 3:
            weekObj = {
              "US": "Wednesday",
              "CN": "星期三",
              "USab": "Wed"
            };
            break;
          case 4:
            weekObj = {
              "US": "Thursday",
              "CN": "星期四",
              "USab": "Thur"
            };
            break;
          case 5:
            weekObj = {
              "US": "Friday",
              "CN": "星期五",
              "USab": "Fri"
            };
            break;
          case 6:
            weekObj = {
              "US": "Saturday",
              "CN": "星期六",
              "USab": "Sat"
            };
            break;
          default:
            weekObj = {
              "US": "Sunday",
              "CN": "星期日",
              "USab": "Sun"
            };
        }
        return flag !== undefined ? weekObj[flag] : weekObj;
      },
      //获取当前月份第一天
      getFirstDayOfMonth: function (flag) {
        return new GetFormatDate(new Date(year, month, 1)).getFormatDate(flag);
      },
      //获取当前月份最后一天
      getLastDayOfMonth: function (flag) {
        var oldYear = year;
        var oldMonth = month;
        if (oldMonth == 12) {
          oldYear++;
          oldMonth = 1;
        } else {
          oldMonth++;
        }
        var lastDateOFMonth = new Date(new Date(oldYear, oldMonth, 1) - timeOfDay);
        return new GetFormatDate(lastDateOFMonth).getFormatDate(flag);
      },
      /**
       * n 代表跳转天数;n为负数代表之前几天,n为正数之后几天
       */
      getAppointDay: function (n, flag) {
        n = n || 0;
        var appointDate = new Date(year, month, days).getTime() + n * timeOfDay;
        return new GetFormatDate(new Date(appointDate)).getFormatDate(flag);
      },
      //计算与指定日期相差的天数
      getDaysOfAppointDate: function (apointDate) {
        if (Object.prototype.toString.call(date) == "[object String]" && date.length === 8) {
          date = new Date(date.substr(0, 4), parseInt(date.substr(4, 2) - 1), date.substr(6, 2));
        }
        if (Object.prototype.toString.call(date) == "[object String]" && date.length === 14) {
          date = new Date(date.substr(0, 4), parseInt(date.substr(4, 2) - 1), date.substr(6, 2), date.substr(8, 2), date.substr(10, 2), date.substr(12, 2));
        }
        if (Object.prototype.toString.call(apointDate) != "[object Date]") {
          throw new Error("参数必须为时间格式Date");
          return false;
        }
        return Math.floor((new Date(year, month, days) - appointDate) / timeOfDay);
      },
      //获取当前月份总天数
      getDaysOfMonth: function () {
        var oldYear = year;
        var oldMonth = month;
        if (oldMonth == 12) {
          oldYear++;
          oldMonth = 1;
        } else {
          oldMonth++;
        }
        return new Date(new Date(oldYear, oldMonth, 1) - timeOfDay).getDate();
      },
      //获取当前年份总天数
      getDaysOfYear: function () {
        var firstOfDateCurrentYear = new Date(year, 0, 1);
        var firstOfDateNextYear = new Date(year + 1, 0, 1);
        return (firstOfDateNextYear - firstOfDateCurrentYear) / timeOfDay;
      },
      //获取该年已经度过天数(不包括当前日期)
      getDaysOfPast: function () {
        var passDays = (new Date(year, month, days) - new Date(year, 0, 1)) / timeOfDay;
        return passDays;
      },
      //获取该年剩余天数(不包括当前日期)
      getDaysOfSurplus: function () {
        return this.getDaysOfYear() - this.getDaysOfPast() - 1;
      },
      //用于保存日期到数据库,startIndex指定索引,num截取长度
      saveSqlStrTime: function (startIndex, num) {
        return this.getFormatTime().replaceAll(/[^0-9]/g, "").substr(startIndex, num);
      }
    }
  }
  

如何直接使用:

console.log(new GetFormatDate(new Date(2020, 0, 1)).getFormatDate());
console.log(new GetFormatDate("20200101").getFormatDate());
console.log(new GetFormatDate("20200101120203").getFormatDate());

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值