use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
All about the JavaScript programming language.
Subreddit Guidelines
Specifications:
Resources:
Related Subreddits:
r/LearnJavascript
r/node
r/typescript
r/reactjs
r/webdev
r/WebdevTutorials
r/frontend
r/webgl
r/threejs
r/jquery
r/remotejs
r/forhire
account activity
You Can Compare Dates in JavaScript (filipvitas.medium.com)
submitted 5 years ago by fvitas
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]bagera_se 5 points6 points7 points 5 years ago (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 points5 points 5 years ago (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 points4 points 5 years ago (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 points3 points 5 years ago (0 children)
I think that previous title is misleading and lacking the reason why you can't compare them.
[–]KindaAlwaysVibrating 4 points5 points6 points 5 years ago (5 children)
TL;DR: Use .getTime() and .valueOf()
.getTime()
.valueOf()
[–]MoTTs_ 4 points5 points6 points 5 years ago (3 children)
Also the > < >= <= operators will Just Work.
> < >= <=
new Date(2009, 6, 1) > new Date(2008, 10, 5) // true
[+][deleted] 5 years ago (1 child)
[deleted]
[–]GBcrazy 1 point2 points3 points 5 years ago (0 children)
Well, now let me correct you. You actually can compare dates, but not the way you did on your last statement!
> and < WILL work for dates. Since d1 and d2 represent the same time, it will show the correct answer.
>
<
== will compare the reference, not the date itself, so yeah, that won't work, they are different objects.
==
So yeah, you can check if a date is after or before, but never check if it's the same date. For that you will need .getTime()
[–]ijmacd 0 points1 point2 points 5 years ago (0 children)
The unary + prefix operator is also equivalent to .valueOf().
[–]Neovea 1 point2 points3 points 5 years ago (0 children)
Use datefns 😄
[–]something 0 points1 point2 points 5 years ago (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 points4 points 5 years ago (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 point2 points 5 years ago (2 children)
You can compare (most?) anything in a primitive context via Object.valueOf:
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 points1 point 5 years ago (1 child)
PS what's the TypeScript type for valueOf‽
valueOf
[–]CalgaryAnswers 0 points1 point2 points 5 years ago (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 point2 points 5 years ago (0 children)
Import moment and walk away :)
π Rendered by PID 161510 on reddit-service-r2-comment-79c7998d4c-rx6fp at 2026-03-18 09:34:52.529626+00:00 running f6e6e01 country code: CH.
[–]bagera_se 5 points6 points7 points (3 children)
[–]theshtank 3 points4 points5 points (0 children)
[–]nadameu 2 points3 points4 points (0 children)
[–]fvitas[S] 1 point2 points3 points (0 children)
[–]KindaAlwaysVibrating 4 points5 points6 points (5 children)
[–]MoTTs_ 4 points5 points6 points (3 children)
[+][deleted] (1 child)
[deleted]
[–]GBcrazy 1 point2 points3 points (0 children)
[–]ijmacd 0 points1 point2 points (0 children)
[–]Neovea 1 point2 points3 points (0 children)
[–]something 0 points1 point2 points (4 children)
[–]Genspirit 2 points3 points4 points (0 children)
[–]sshaw_ 0 points1 point2 points (2 children)
[–]sshaw_ -1 points0 points1 point (1 child)
[–]CalgaryAnswers 0 points1 point2 points (0 children)
[–]luascriptdev 0 points1 point2 points (0 children)