美文网首页@IT·互联网
使用moment计算一个时间区间有多少年,多少月,多少天

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

作者: 燕自浩 | 来源:发表于2024-06-24 16:28 被阅读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));
  }
}


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

相关文章

  • JS 计算两个时间戳相差年月日时分秒

    // 计算两个时间戳相差的多少年多少月多少天 calculateDiffTime() { let startTi...

  • 生命的意义

    最近下载了一个手机APP-生辰。会计算你在世上活了多少年,多少月,多少天,多少分钟,看着秒针一点一点的跳动,感觉生...

  • JS计算两个时间间隔

    1 前言 1.1 业务场景 JavaScript计算两个时间相隔了 多少年多少月多少日。时分秒这里不作考虑。 2 ...

  • python实现leetcode之115. 不同的子序列

    解题思路 使用缓存避免重复计算定义一个函数,计算s的某区间包含t的某区间多少次细节如代码中的注释 115. 不同的...

  • 多少

    多少年多少月多少天 多少做过的梦不见 多少心被伤了多少遍 多少距离多么遥远 多少少年渴望着情缘 多少梦想无法实现 ...

  • 无言的沉默               原创

    多少年的努力, 多少年的付出, 多少年的拼搏, 多少年的期盼, 多少年的日出而做,日落而息。 多少年的披星戴月,万...

  • 别来无恙

    多少天 还有多少天 值得我们收藏 多少年 还有多少年 敌得过徒劳感伤 宿命蛰伏在伤口 等着 醒来后的痛苦 走过几度...

  • 我站在了窗前望,多少年的风景沉淀多少年的时光,多少年的明月泛滥多少年的相思,多少的无情神伤了多少痴情,多少年少轻狂...

  • 后来的我们

    多少年 多少年 磨失了童年 改换了视野 多少天 多少天 经历了风雨 走向了晴天 当年我们会唱着童谣牵手春游 现...

  • 这6个让你相见恨晚的APP请带走

    1、生辰 可以计算出生到现在活了多少年、月、周……还可以计算距离你预测的死亡日期还有多久并显示出还能做多少事情 2...

网友评论

    本文标题:使用moment计算一个时间区间有多少年,多少月,多少天

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