r/learnjavascript • • May 11 '22

Comparing two dates in JavaScript is easier than you may think.

Post image
206 Upvotes

15 comments sorted by

12

u/shuckster May 11 '22

You could use coercion to shortcut it:

+date1 === +date2
// true

As you can see, coercing to a number implicitly invokes getTime().

9

u/elediardo May 11 '22

I learned something new today. Thank you!

47

u/toastertop May 11 '22

Coercion is clever, clarity is cleaner.

0

u/burnblue May 11 '22

Cleaner isn't the word I'd use; as a matter of fact the coerced version could be described as cleaner looking. The getTime version is just unmistakable and wise.

But even then it could be argued that a reader might be unaware that getTime returns an integer (seconds since epoch) but are fully aware that '+' makes an integer just like it does on every other variable, datetime or not. I actually thought for a second that getTime might have given some formatted timestamp string. But +date is a number for sure, as long as you've ever seen coercion

11

u/eeeBs May 11 '22 edited May 11 '22

What is this conversion witch craft/socery/blood magic?

Edit: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Unary_plus

TIL

2

u/TehTriangle May 11 '22

😰 Thank god I work in Typescript these days...

2

u/shuckster May 11 '22

Doesn’t seem to make any difference.

32

u/burkybang May 11 '22

FYI, the month you set is June not May because it’s 0 based.

1

u/iiMoe May 11 '22

That's rly weird and confusing, appreciate the info

7

u/[deleted] May 11 '22

[removed] — view removed comment

9

u/Marshallo May 11 '22

For others reading, it's because the day and month value is a key to represent a string ('Tuesday' or 'June' for example) whereas the day of the month (11) has no text version so can be 1-based.

It makes it nicer to do this:

month = ['Jan', 'Feb', 'Mar', etc...][date.getMonth()]

2

u/shiningmatcha May 11 '22

How about using == instead of ===? I think this way JS will compare the values returned by date1.valueOf() and date2.valueOf().

3

u/Patient_Signature467 May 11 '22

Because you are not comparing values but reference. date1 has a different location (reference) in the memory as date 2 even though their values are the same. In the second example that returns true, you are comparing actual values.

Look up on youtube reference vs value in javascript, if you get a visual explanation it will be easier to understand.

0

u/[deleted] May 11 '22

[deleted]

6

u/xroalx May 11 '22

Because there are other languages that allow such comparison, or allow operator overloading. JavaScript isn't one of them, but not everybody has to necessarily be aware of that.

For example, the C# equivalent of Date is DateTime, which overrides the comparison operators, meaning new DateTime() == new DateTime() won't be comparing different objects, it will in fact compare the date and time they represent.

0

u/[deleted] May 11 '22

[deleted]

0

u/[deleted] May 11 '22

[deleted]

6

u/StoneCypher May 11 '22

it's correct syntax that doesn't do what most people expect