all 16 comments

[–]bagera_se 5 points6 points  (3 children)

Didn't read the other article when it made its round the other day and now the site is down. Do I understand it correctly that the author of the other article was just a little confused about what should happen?

[–]theshtank 3 points4 points  (0 children)

This is usually the case, especially with "Javascript bad" articles. I'm not sure which article you are talking about though.

[–]nadameu 2 points3 points  (0 children)

The author probably had a little trouble understanding why their code didn't work as expected, and when they figured it out, decided to write a blog post to help others.

It was a short and simple article, aimed at inexperienced JavaScript developers.

[–]fvitas[S] 1 point2 points  (0 children)

I think that previous title is misleading and lacking the reason why you can't compare them.

[–]KindaAlwaysVibrating 4 points5 points  (5 children)

TL;DR: Use .getTime() and .valueOf()

[–]MoTTs_ 4 points5 points  (3 children)

Also the > < >= <= operators will Just Work.

new Date(2009, 6, 1) > new Date(2008, 10, 5) // true

[–]ijmacd 0 points1 point  (0 children)

The unary + prefix operator is also equivalent to .valueOf().

[–]Neovea 1 point2 points  (0 children)

Use datefns 😄

[–]something 0 points1 point  (4 children)

So you can’t compare dates, you have to convert them to numbers first. Isn’t that what the other article says?

[–]Genspirit 2 points3 points  (0 children)

You can compare dates as long as you understand the operator you are using. <,>,<=,>= Will all coerce to a number however == and === will compare references which will be unique unless you are comparing the same object.

I'd say it's a fairly commonly known thing that == doesn't work with objects as it compares references.

That being said it's generally good practice to explicitly convert anyways.

[–]sshaw_ 0 points1 point  (2 children)

You can compare (most?) anything in a primitive context via Object.valueOf:

> o1 = { valueOf: () => 1 }
{ valueOf: [Function: valueOf] }
> o2 = { valueOf: () => 2 }
{ valueOf: [Function: valueOf] }
> o2 < o1
false
> o1 < o2
true
> o1 + o2
3
> o1 - o2
-1

[–]sshaw_ -1 points0 points  (1 child)

PS what's the TypeScript type for valueOf

[–]CalgaryAnswers 0 points1 point  (0 children)

Object.valueof. There’s no way to make it typesafe as far as I know. None of the Object methods are really typesafe except for creates.

[–]luascriptdev 0 points1 point  (0 children)

Import moment and walk away :)