时间封装

export class DateTime {
  timeofsubtle: number = 0;

  constructor(time: Date | string) {
    if (typeof time === "string") {
      time = new Date(time);
    }
    this.timeofsubtle = time.getTime();
  }
  static parse(time: Date | string): DateTime {
    return new this(time);
  }

  /**
   * 获取时间微秒
   * @returns
   */
  getTimeofsubtle(): number {
    return this.timeofsubtle;
  }

  /**
   * 获取时间戳
   * @returns
   */
  getTimestamp(): number {
    return Number(this.timeofsubtle / 1000);
  }

  /**
   * 格式化时间
   * @param format
   * @returns
   */
  format(format: string = "YYYY-MM-DD HH:mm:ss"): string {
    const date = new Date(this.timeofsubtle);
    const config = {
      YYYY: date.getFullYear(),
      MM: date.getMonth() + 1,
      DD: date.getDate(),
      HH: date.getHours(),
      mm: date.getMinutes(),
      ss: date.getSeconds(),
    } as any;
    for (const key in config) {
      config[key] = config[key].toString().padStart(key.length, "0");
      format = format.replace(key, config[key] ?? "");
    }
    return format;
  }

  /**
   * 增加多个月
   * @param months
   */
  addMonths(months: number = 1): void {
    const date = new Date(this.timeofsubtle);
    let currentDay = date.getDate();
    date.setDate(1);
    date.setMonth(date.getMonth() + months);
    this.timeofsubtle = date.getTime();
    const maxDay = this.currentMonthMaxDay();
    if (currentDay > maxDay) {
      currentDay = maxDay;
    }
    date.setDate(currentDay);
    this.timeofsubtle = date.getTime();
  }

  /**
   * 减去多个月
   * @param months
   */
  subMonths(months: number = 1): void {
    const date = new Date(this.timeofsubtle);
    let currentDay = date.getDate();
    date.setDate(1);
    date.setMonth(date.getMonth() - months);
    this.timeofsubtle = date.getTime();
    const maxDay = this.currentMonthMaxDay();
    if (currentDay > maxDay) {
      currentDay = maxDay;
    }
    date.setDate(currentDay);
    this.timeofsubtle = date.getTime();
  }

  /**
   * 获取这个月的开始时间(不会改变原时间)
   * @param format
   * @returns
   */
  startMonths(format: string = "YYYY-MM-DD 00:00:00"): string {
    return DateTime.parse(this.format("YYYY-MM-01 00:00:00")).format(format);
  }

  /**
   * 获取这个月的结束时间(不会改变原时间)
   * @param format
   * @returns
   */
  endMonths(format: string = "YYYY-MM-DD 23:59:59"): string {
    const date = new Date(this.timeofsubtle);
    date.setDate(this.currentMonthMaxDay());
    return DateTime.parse(date).format(format);
  }

  /**
   * 获取当前时间的当前月的最大天数
   * @returns
   */
  currentMonthMaxDay(): number {
    const currentMonth = DateTime.parse(this.format("YYYY-MM-01 00:00:00"));
    const startTimeofsubtle = currentMonth.getTimeofsubtle();
    const endDate = DateTime.parse(this.format("YYYY-MM-01 00:00:00")).format();
    const newDate = new Date(endDate);
    newDate.setMonth(newDate.getMonth() + 1);
    const endTimeofsubtle = newDate.getTime();
    const currentDays =
      (endTimeofsubtle - startTimeofsubtle) / 1000 / 24 / 60 / 60;
    return currentDays;
  }

  addHours(hours: number = 1): void {
    this.timeofsubtle = this.timeofsubtle + 60 * 60 * 1000 * hours;
  }
  subHours(hours: number = 1): void {
    this.timeofsubtle = this.timeofsubtle - 60 * 60 * 1000 * hours;
  }
  addDays(days: number = 1): void {
    this.timeofsubtle = this.timeofsubtle + 24 * 60 * 60 * 1000 * days;
  }
  subDays(days: number = 1): void {
    this.timeofsubtle = this.timeofsubtle - 24 * 60 * 60 * 1000 * days;
  }
  addMinutes(minutes: number = 1): void {
    this.timeofsubtle = this.timeofsubtle + 60 * 1000 * minutes;
  }
  subMinutes(minutes: number = 1): void {
    this.timeofsubtle = this.timeofsubtle + 60 * 1000 * minutes;
  }

  /**
   * 获取开始天(这里不会改变原时间)
   * @param format
   * @returns
   */
  startDay(format: string = "YYYY-MM-DD HH:mm:ss"): string {
    return DateTime.parse(this.format("YYYY-MM-DD 00:00:00")).format(format);
  }

  /**
   * 获取结束天(这里不会改变原时间)
   * @param format
   * @returns
   */
  endDay(format: string = "YYYY-MM-DD HH:mm:ss"): string {
    return DateTime.parse(this.format("YYYY-MM-DD 23:59:59")).format(format);
  }
}
Last Updated:
Contributors: 刘荣杰