@IT·互联网

使用moment计算一个时间区间有多少年,多少月,多少天

2024-06-24  本文已影响0人  燕自浩

前言:当我们要根据一个开始时间和一个结束时间来结算这个时间区间内有多少年,多少月,多少天时怎么处理可以比较准确的计算出我们想要的数据。遇到这个问题时我首先想到了momentdiff,看了下官方的文档是diff结合duration使用,这样我遇到了一个问题当开始时间是2024-3-1结束时间是2025-1-31时使用diff结合duration计算的结果是月数是11,天数是2,这样的结果出乎我的意料,也不是我们期望的结果。

  const startDate = '2024-3-1';
  const endDate = '2025-1-31';
  const currentDate = moment(startDate, 'YYYY-MM-DD');
  const futureDate = moment(endDate).add(1, 'days');
  const diff = moment.duration(futureDate.diff(currentDate));

  console.log(`${diff.months()} Months and ${diff.days()} Days`);

打印如下结果


示例1

diff我们是必须要用到的api,接下来说明如何计算出符合我们预期的结果

  const startDate = '2024-3-1';
  const endDate = '2025-1-31';
  const start = moment(startDate);
  const end = moment(endDate).add(1, 'days');

  const years = end.diff(start, 'year');
  start.add(years, 'years');

  const months = end.diff(start, 'months');
  start.add(months, 'months');

  const days = end.diff(start, 'days');

我们把这个封装成一个拿来即用的函数如下:

export const diffDate = (startDate: string, endDate: string) => {
  const start = moment(startDate);
  const end = moment(endDate).add(1, 'days');

  const years = end.diff(start, 'year');
  start.add(years, 'years');

  const months = end.diff(start, 'months');
  start.add(months, 'months');

  const days = end.diff(start, 'days');

  return {
    years,
    months,
    days
  }
}

记录下我们老大封装的

import dayjs from 'dayjs';
import duration from 'dayjs/plugin/duration';
dayjs.extend(duration);

export default class ContextualDuration {
  private _start: dayjs.Dayjs;
  private _end: dayjs.Dayjs;
  years: number;
  months: number;
  days: number;
  hours: number;
  minutes: number;
  seconds: number;
  milliseconds: number;

  constructor(start: Parameters<typeof dayjs>[0], end: Parameters<typeof dayjs>[0]) {
    this._start = dayjs(start);
    this._end = dayjs(end);

    let _start = this._start.clone();
    this.years = this._end.diff(_start, 'year');
    _start = _start.add(this.years, 'years');

    this.months = this._end.diff(_start, 'months');
    _start = _start.add(this.months, 'months');

    this.days = this._end.diff(_start, 'days');
    _start = _start.add(this.days, 'days');

    this.hours = this._end.diff(_start, 'hours');
    _start = _start.add(this.hours, 'hours');

    this.minutes = this._end.diff(_start, 'minute');
    _start = _start.add(this.minutes, 'minute');

    this.seconds = this._end.diff(_start, 'seconds');
    _start = _start.add(this.seconds, 'seconds');

    this.milliseconds = this._end.diff(_start, 'milliseconds');
  }

  toJSON() {
    return {
      years: this.years,
      months: this.months,
      days: this.days,
      hours: this.hours,
      minutes: this.minutes,
      seconds: this.seconds,
      milliseconds: this.milliseconds,
    };
  }

  format(format?: string) {
    return dayjs.duration(this.toJSON()).format(format);
  }

  toDuration() {
    return dayjs.duration(this._end.diff(this._start));
  }
}


到这里就结束了,遇见即美好!

上一篇 下一篇

猜你喜欢

热点阅读