0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Date型の比較でやりがちなミスのメモ

Last updated at Posted at 2023-12-13

Date型どうしで同値か比較したい

そのまま比較するとfalseとなってしまう。

let date1 = new Date('2023/12/13 19:30');
let date2 = new Date('2023/12/13 19:30');

date1 === date2 // false

date1とdate2は別のインスタンスなのでfalseと判定される

対策

let date1 = new Date('2023/12/13 19:30');
let date2 = new Date('2023/12/13 19:30');

date1.getTime() === date2.getTime() // true

.getTime()でミリ秒単位の数値に変換して比較する

0
0
1

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?