在 JavaScript 中减去两个日期时,它会给出一个十进制数,但这怎么可能呢?

发布于 2025-01-17 05:31:12 字数 397 浏览 0 评论 0原文

我正在观看一个视频,其中两个日期之间的差异返回 10,但当我尝试时,它给我一个十进制数,但这是为什么呢?我可以将其包装在 Math.floor() 中,但如果有人解释我,我将不胜感激。 这是代码

const calcDaysPassed = (date1, date2) =>
  Math.abs(date2 - date1) / (1000 * 60 * 60 * 24);

const days1 = calcDaysPassed(
  new Date(2037, 3, 4),
  new Date(2037, 3, 14)
);
console.log(days1);

I'm watching a video where the diff between two dates returns 10 but when I try it gives me a decimal number but why is that? I could wrap it in Math.floor() but I'd appreciate if anyone explain me.
Here is the code

const calcDaysPassed = (date1, date2) =>
  Math.abs(date2 - date1) / (1000 * 60 * 60 * 24);

const days1 = calcDaysPassed(
  new Date(2037, 3, 4),
  new Date(2037, 3, 14)
);
console.log(days1);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

南风几经秋 2025-01-24 05:31:12

日期对象表示时间上的单个瞬间,并且只是相对于 UTC 1970 年 1 月 1 日的偏移量的时间值。

减法运算符强制 Date 对象进行编号,就像通过 date.valueOf() 相当于 date.getTime(),因此当您从一个日期中减去另一个日期时,您会得到它们时间值的差异,例如:

dateA - dateB === dateA.getTime() - dateB.getTime()

因此在您的代码中:

new Date(2037, 3, 4) - new Date(2037, 3, 14);

返回差异毫秒:

-864000000

即 10 个标准天。

请注意,传递给上面 Date 构造函数的值被解释为本地值,因此如果日期范围内存在夏令时转换,则差异可能或多或少取决于夏令时转换的量(通常为 1 小时,但在某些地方为 30分钟)。

有很多类似的问题:

  1. Get Difference between 2 dates in JavaScript?
  2. 获取两个日期之间的时差(以秒为单位)
  3. 如何计算两个日期之间的天数?

Date objects represent a single instant in time and are just a time value that is an offset from 1 Jan 1970 UTC.

The subtraction operator coerces the Date objects to number as if by date.valueOf() which is equivalent to date.getTime(), so when you subtract one date from another you get the difference in their time values such that:

dateA - dateB === dateA.getTime() - dateB.getTime()

So in your code:

new Date(2037, 3, 4) - new Date(2037, 3, 14);

returns the difference in milliseconds:

-864000000

which is 10 standard days.

Note that the values passed to the Date constructor above are interpreted as local so if there is a daylight saving changeover in the date range the difference might be more or less by the amount of the daylight saving shift (typically 1 hour but in some places 30 minutes).

There are lots of similar questions:

  1. Get difference between 2 dates in JavaScript?
  2. Get time difference between two dates in seconds
  3. How to calculate number of days between two dates?
舞袖。长 2025-01-24 05:31:12

Date 对象包含一个 Number,表示自 UTC 1970 年 1 月 1 日以来的毫秒数。” MDN

date2 - date1 返回以下差异毫秒。 MDN:

Date 对象的 [@@toPrimitive]() 方法返回一个基元
值,其类型为 numberstring 类型。

如果hintstringdefault[@@toPrimitive]()尝试调用
toString 方法。如果 toString 属性不存在,它会尝试
调用 valueOf 方法,如果 valueOf 也不存在,
[@@toPrimitive]() 抛出 TypeError

如果hintnumber[@@toPrimitive]()首先尝试调用valueOf,并且
如果失败,它会调用 toString

JavaScript 调用 [@@toPrimitive]() 方法将对象转换为
一个原始值。您很少需要调用 [@@toPrimitive]()
自己方法; JavaScript 遇到以下情况时会自动调用它
需要原始值的对象。

Math.abs(date2 - date1) / (1000 * 60 * 60 * 24) 将毫秒转换为天。

"Date objects contain a Number that represents milliseconds since 1 January 1970 UTC." MDN

date2 - date1 returns the difference in milliseconds. The conversion is described in MDN:

The [@@toPrimitive]() method of the Date object returns a primitive
value, that is either of type number or of type string.

If hint is string or default, [@@toPrimitive]() tries to call the
toString method. If the toString property does not exist, it tries to
call the valueOf method and if the valueOf does not exist either,
[@@toPrimitive]() throws a TypeError.

If hint is number, [@@toPrimitive]() first tries to call valueOf, and
if that fails, it calls toString.

JavaScript calls the [@@toPrimitive]() method to convert an object to
a primitive value. You rarely need to invoke the [@@toPrimitive]()
method yourself; JavaScript automatically invokes it when encountering
an object where a primitive value is expected.

Math.abs(date2 - date1) / (1000 * 60 * 60 * 24) converts the milliseconds to days.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文