datehelper

70 阅读2分钟
/**
 * @desc 日期格式化等操作助手  YYYY MM DD HH:mm:ss
 * ex: new DateHelper('2022-09').getFirstDayByUnit()  new DateHelper(new Date()).format('YYYY年M月D日')  new DateHelper(new Date()).isAfter('2023-08-31 10:59:59')
 */

import dayjs from 'dayjs';
import isBetween from 'dayjs/plugin/isBetween'; // 引入相关插件
dayjs.extend(isBetween);

// 格式化字符串日期
export const formatDateStr = (dateStr, format = 'YYYY-MM-DD') => {
  return dayjs(dateStr).format(format);
}

export default class DateHelper {
  /**
   * @desc 初始化
   * @param {string | object}  dateString: 日期字符串 或者 日期对象
   */
  constructor(dateString) {
    this.date = dayjs(dateString);
  }

  /**
   * @desc 格式化时间
   * @param {string}  formatString: 需要格式化的字符串  YYYY年M月D日  YYYY-MM-DD HH:mm:ss
   * @return {string}  格式化后的时间
   */
  format(formatString) {
    return this.date.format(formatString);
  }

  /**
   * @desc 加
   * @param {number} num:步长
   * @param {string} unit:单位 millisecond second minute hour  day  week month  year
   * @param {string} formatString:'YYYY-MM-DD'
   * @return {string} 加之后的结果
   */
  add(num, unit = 'day', formatString = 'YYYY-MM-DD') {
    return this.date.add(num, unit).format(formatString);
  }

  /**
   * @desc 减
   * @param {number} num:步长
   * @param {string} unit:单位 millisecond second minute hour  day  week month  year
   * @param {string} formatString:'YYYY-MM-DD'
   * @return {string} 减之后的结果
   * @example Mon Dec 04 2023 14:34:38 GMT+0800 (中国标准时间) ===>>> 2023-11-29
   */
  subtract(num, unit = 'day', formatString = 'YYYY-MM-DD') {
    return this.date.subtract(num, unit).format(formatString);
  }

  /**
   * @desc 获取某年(某月)的第一天
   * @param {string}  unit: month  year
   * @param {string}  formatString:'YYYY-MM-DD'
   * @return {string}  某年(某月)的第一天
   * @example Mon Dec 04 2023 14:34:38 GMT+0800 (中国标准时间) ===>>> 2023-12-01
   */
  getFirstDayByUnit(unit = 'month', formatString = 'YYYY-MM-DD') {
    return this.date.startOf(unit).format(formatString);
  }

  /**
   * @desc 获取某年(某月)的最后一天
   * @param {string}  unit: month year
   * @param {string}  formatString: month year
   * @return {string} 某年(某月)的最后一天
   */
  getLastDayByUnit(unit = 'month', formatString = 'YYYY-MM-DD') {
    return this.date.endOf(unit).format(formatString);
  }

  /**
   * @desc 获取时间差
   * @param {object} otherDate:日期对象
   * @param {string} unit: month year
   * @return {number} 时间差
   */
  getDiff(otherDate, unit = 'day') {
    return this.date.diff(otherDate, unit);
  }

  /**
   * @desc 判断一个日期是否在另外一个日期之后
   * @param {object} otherDate:日期对象
   * @return {boolean} true:之后 false:之前
   */
  isAfter(otherDate) {
    return this.date.isAfter(otherDate);
  }

  /**
   * @desc 判断一个日期是否在另外一个日期之前
   * @param {object} otherDate:日期对象
   * @return {boolean} true:之前 false:之后
   */
  isBefore(otherDate) {
    return this.date.isBefore(otherDate);
  }

  /**
   * @desc 判断一个日期是否在两个日期之间
   * @param {object} a:日期对象
   * @param {object} b:日期对象
   * @return {boolean} true:是
   */
  isBetween(a, b) {
    return this.date.isBetween(a, b);
  }

  /**
   * @desc 获取一个日期是周几
   * @return {string} 周六
   */
  getDayOfWeek() {
    // const daysOfWeek = ['Sunday', 'Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday'];
    const daysOfWeekChinese = ['周日', '周一', '周二', '周三', '周四', '周五', '周六'];
    const dayOfWeek = this.date.day(); // 获取日期的星期几,返回值为 0(周日)、1(周一)... 5 (周五)、 6(周六)
    return daysOfWeekChinese[dayOfWeek];
  }
}