这些日期为什么不匹配?

huangapple go评论76阅读模式
英文:

Why do this dates not match?

问题

我有这段处理日期的代码:

let startDate = new Date(toolOrder.jsAppStartDate.getTime()); // 克隆日期
startDate.setDate(startDate.getDate() - Math.floor((days - 1) / 2)); // 减去一些天数
console.log(toolOrder.jsAppStartDate); // 输出:Date Thu Sep 21 2023 00:00:00 GMT+0200 (中欧夏令时间)
console.log(toolOrder.jsAppStartDate.getTime()); // 输出:1695247200000
console.log("--------------");
for (let index = 0; index < days; index++) {
    console.log(startDate);
    console.log(startDate.getTime());
    // ... 这里是一些绘图代码 ...
    startDate.setDate(startDate.getDate() + 1); // 增加一天
    if (startDate === toolOrder.jsAppStartDate) // 检查是否达到起始日期
    {
        console.log("日期匹配");
        startDate = new Date(toolOrder.jsAppEndDate.getTime());
    }
}

循环之前的日志输出是:

Date Thu Sep 21 2023 00:00:00 GMT+0200 (中欧夏令时间)
1695247200000
---------------

循环中的输出是:

Date Mon Sep 18 2023 00:00:00 GMT+0200 (中欧夏令时间)
1694988000000 
Date Tue Sep 19 2023 00:00:00 GMT+0200 (中欧夏令时间)
1695074400000
Date Wed Sep 20 2023 00:00:00 GMT+0200 (中欧夏令时间)
1695160800000
Date Thu Sep 21 2023 00:00:00 GMT+0200 (中欧夏令时间)
1695247200000
Date Fri Sep 22 2023 00:00:00 GMT+0200 (中欧夏令时间)
1695333600000

因此,即使在循环的第3次迭代中日期匹配,if条件仍然不成立。当我使用以下代码时它才起作用:

if (startDate.getTime() === toolOrder.jsAppStartDate.getTime())

但我认为它不应该需要这样做,它应该可以工作对吗?

英文:

i have this code which works with dates:

let startDate = new Date(toolOrder.jsAppStartDate.getTime()); // clone date
startDate.setDate(startDate.getDate() - Math.floor((days - 1) / 2)); // sub some days
console.log(toolOrder.jsAppStartDate); // Output: Date Thu Sep 21 2023 00:00:00 GMT+0200 (Central European Summer Time)
console.log(toolOrder.jsAppStartDate.getTime()); // Output: 1695247200000
console.log(&quot;--------------&quot;);
for (let index = 0; index &lt; days; index++)
{
    console.log(startDate);
    console.log(startDate.getTime());
    // ... some drawing code here ...
    startDate.setDate(startDate.getDate() + 1); // add one day
    if(startDate === toolOrder.jsAppStartDate) // check if start date reached
    {
        console.log(&quot;Dates match&quot;);
        startDate = new Date(toolOrder.jsAppEndDate.getTime());
    }
}

The output of the log before the loop is:

Date Thu Sep 21 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695247200000
---------------

The output from the loop is:

Date Mon Sep 18 2023 00:00:00 GMT+0200 (Central European Summer Time)
1694988000000 
Date Tue Sep 19 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695074400000
Date Wed Sep 20 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695160800000
Date Thu Sep 21 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695247200000
Date Fri Sep 22 2023 00:00:00 GMT+0200 (Central European Summer Time)
1695333600000

So even if the dates match in loop 3, the if condition is not true. It works when I use

if(startDate.getTime() === toolOrder.jsAppStartDate.getTime())

but I thought it should work without also?

答案1

得分: 2

=== 比较身份,所以:

startDate === toolOrder.jsAppStartDate

意味着:startDatetoolOrder.jsAppStartDate 在内存中是相同的对象吗?不是,因为 toolOrder.jsAppStartDate 是一个不同的对象,而不是由名称 startDate 引用的对象。

mdn 关于严格相等运算符的说法是:

> 如果两个操作数都是对象,仅当它们引用相同的对象时才返回true

(重点在"mine"上)

if(startDate.getTime() === toolOrder.jsAppStartDate.getTime())

这个工作是因为现在你在比较数字而不是对象。

两个操作数是数字的规则是:

> 否则,比较两个操作数的值:
>
> 数字必须具有相同的数值。 +0 和 -0 被视为相同的值。

mdn (重点在"mine"上)

英文:

=== compares identity so:

startDate === toolOrder.jsAppStartDate

Means: is startDate the same object [in memory] as toolOrder.jsAppStartDate which is not the case because toolOrder.jsAppStartDate is a different object and not the object referred to by the name startDate.

mdn says this about the strict equality operator:

> If both operands are objects, return true only if they refer to the same object.

(emphasis mine)

if(startDate.getTime() === toolOrder.jsAppStartDate.getTime())

Works because you're now comparing numbers instead of objects.

The rule for the two operands being numbers is:

> Otherwise, compare the two operand's values:
>
> Numbers must have the same numeric values. +0 and -0 are considered to be the same value.

mdn (emphasis mine)

答案2

得分: 2

你正在使用===运算符比较Date实例,但它们并不是同一个实例。每次使用new Date都会创建一个独特的新实例(除非类构造函数有意返回先前创建的实例[更多信息请参阅singleton pattern])。

当你比较每个实例的getTime()结果时,实际上在比较两个基本值(在这种情况下是数字)。基本值不是类实例,因此===的行为类似于与基本值的==

英文:

You are comparing Date instances with each other when you're using the === operator. However, they are not the same instance. Each new Date creates a unique, new instance (unless the class constructor deliberately returns a previously created instance [for more info: singleton pattern]).

When you are comparing each instance's getTime() result, you are comparing 2 primitive values (numbers in this case). Primitive values are not class instances; so === works like == with primitives.

huangapple
  • 本文由 发表于 2023年1月8日 22:22:15
  • 转载请务必保留本文链接:https://go.coder-hub.com/75048482.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定