JS 时间工具类

作者: 极简博客 | 来源:发表于2022-08-10 14:24 被阅读0次

由于在项目中使用到时间差,起初是通过一个月30天一年365天去计算的,但是会存在误差,又不想引入第三方js,于是自己实现一个时间工具类。

const date = {
      // 年月日时分秒单位
      YEAR: 'year',
      MONTH: 'month',
      DATE: 'date',
      HOURS: 'hours',
      MINUTES: 'minutes',
      SECONDS: 'seconds',
      // 时间差,单位:年月日时分秒,注意:单位不存在返回-1
      diff: function (startDate, endDate, unit) {
        function days(year, month) {
          let days = 30;
          if (month === 2) {
            days = year % 4 === 0 ? 29 : 28;
          } else if (month === 1 || month === 3 || month === 5 || month === 7 || month === 8 || month === 10 || month === 12) {
            days = 31;
          }
          return days;
        }
        let startParse = this.parse(startDate);
        let endParse = this.parse(endDate);
        let diffTimestamp = endDate - startDate;
        let diffVal = -1;
        if (unit == this.YEAR || unit == this.MONTH) {
          const diffYear = endParse.year - startParse.year;
          const startDays = days(startParse.year, startParse.month);
          const endDays = days(endParse.year, endParse.month);
          const diffStartMonth = (startDays - (startParse.date + ((startParse.hours * 60 + startParse.minutes + startParse.seconds / 60) / 60 / 24) - 1)) / startDays;
          const diffEndMonth = (endParse.date + ((endParse.hours * 60 + endParse.minutes + endParse.seconds / 60) / 60 / 24) - 1) / endDays;
          const diffMonth = diffStartMonth + diffEndMonth + (12 - startParse.month - 1) + endParse.month + (diffYear - 1) * 12;
          diffVal = unit == this.YEAR ? diffMonth / 12 : diffMonth;
        } else if (unit == this.DATE) {
          diffVal = parseInt((diffTimestamp / 1000 / 60 / 60 / 24) + '');
        } else if (unit == this.HOURS) {
          diffVal = parseInt((diffTimestamp / 1000 / 60 / 60) + '');
        } else if (unit == this.MINUTES) {
          diffVal = parseInt((diffTimestamp / 1000 / 60) + '');
        } else if (unit == this.SECONDS) {
          diffVal = parseInt((diffTimestamp / 1000) + '');
        }
        return Math.abs(diffVal);
      },
      // 计算时间 减法,参数错误范围传入时间
      sub: function (date, number, unit) {
        if (!date || !number || !unit) {
          return date;
        }
        let data = new Date(date);
        switch (unit) {
          case this.YEAR: data.setFullYear(data.getFullYear() - number); break;
          case this.MONTH: data.setMonth(data.getMonth() - number); break;
          case this.DATE: data.setDate(data.getDate() - number); break;
          case this.HOURS: data.setHours(data.getHours() - number); break;
          case this.MINUTES: data.setMinutes(data.getMinutes() - number); break;
          case this.SECONDS: data.setSeconds(data.getSeconds() - number); break;
        }
        return data;
      },
      // 计算时间:溢出设置为当前时间 减法
      sub_overflow: function (date, number, unit) {
        let now = new Date();
        let newDate = this.sub(date, number, unit);
        return newDate > now ? now : newDate;
      },
      // 计算时间 加法
      add: function (date, number, unit) {
        return this.sub(date, -number, unit);
      },
      // 计算时间:溢出设置为当前时间 加法
      add_overflow: function (date, number, unit) {
        return this.sub_overflow(date, -number, unit);
      },
      // 转换为时间对象:年月日时分秒
      parse: function (date) {
        return {
          "year": date.getFullYear(),
          "month": date.getMonth() + 1,
          "date": date.getDate(),
          "hours": date.getHours(),
          "minutes": date.getMinutes(),
          "seconds": date.getSeconds()
        };
      },
      // 转换为日期字符串格式:2022-08-10
      parseDateStr: function (date) {
        let parse = this.parse(date);
        return [parse.year, parse.month < 10 ? '0' + parse.month : parse.month, parse.date < 10 ? '0' + parse.date : parse.date].join('-');
      },
      // 转换为时间字符串格式:2022-08-10 10:00:00
      parseDateTimeStr: function (date) {
        let parse = this.parse(date);
        return [parse.year, parse.month < 10 ? '0' + parse.month : parse.month, parse.date < 10 ? '0' + parse.date : parse.date].join('-')
            .concat(' ' + [parse.hours < 10 ? '0' + parse.hours : parse.hours, parse.minutes < 10 ? '0' + parse.minutes : parse.minutes, parse.seconds < 10 ? '0' + parse.seconds : parse.seconds].join(':'));
      }
    }

相关文章

网友评论

    本文标题:JS 时间工具类

    本文链接:https://www.haomeiwen.com/subject/rshawrtx.html