Skip to content

Latest commit

 

History

History
31 lines (22 loc) · 958 Bytes

getMonthsDiffBetweenDates.md

File metadata and controls

31 lines (22 loc) · 958 Bytes
标题 标签
getMonthsDiffBetweenDates(获取日期之间的月份差) date(日期)

计算两个日期之间的差异(以月为单位)。

  • 使用 Date.prototype.getFullYear() 和 Date.prototype.getMonth() 计算两个 Date 对象之间的差异(以月为单位)。
const getMonthsDiffBetweenDates = (dateInitial, dateFinal) =>
  Math.max(
    (dateFinal.getFullYear() - dateInitial.getFullYear()) * 12 +
      dateFinal.getMonth() -
      dateInitial.getMonth(),
    0
  );

调用方式:

getMonthsDiffBetweenDates(new Date('2017-12-13'), new Date('2018-04-29')); // 4

应用场景

结果如下:

<iframe src="codes/javascript/html/getMonthsDiffBetweenDates.html"></iframe>