0
  1. '2012-12-12' < '2013-11-12' // true

  2. new Date('2012-12-12') < new Date('2013-11-12') // true

Both produce same result. But when I search some example code comparing string format date, everyone convert it to date type. Then first example can be problem , But I am not sure when it can be problem. Let me know more detail. please!

Sam Kim
  • 113
  • 1
  • 9
  • Does this answer your question? [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – Kinglish Jan 06 '22 at 00:31

2 Answers2

0

'2012-12-12' < '2013-11-12' // true

Compares the dates as strings. One benefit of ISO 8601 formatting is that timestamps compare correctly as strings provided the offset (if there is one) is the same. E.g.

'2022-01-06T09:00:00+10:00' < '2022-01-06T00:00:00Z'

should be true, but it returns false, whereas:

new Date('2022-01-06T09:00:00+10:00') < new Date('2022-01-06T00:00:00Z')

returns true (see below for why). But comparing other date formats as strings almost certainly won't work, e.g. using d/m/y format:

01/03/2020 < 01/02/2021

returns false because the "3" in "03" is greater than the "2" in "02" and that's as far as the comparison gets.

Then there is:

new Date('2012-12-12') < new Date('2013-11-12') // true

which works because both timestamps are parsed as UTC to produce a time value (a number value that is the date's offset from 1 Jan 1970 in milliseconds). The < operator coerces the Date objects to primitives which, in this case, effectively calls valueOf which returns the time value, hence the comparison is reduced to:

1355270400000 < 1384214400000

That's why strings are often parsed to Date objects for comparison. Note however that while:

'2022-01-06' == '2022-01-06' // true

When converted to Dates:

new Date('2022-01-06') == new Date('2022-01-06') // false

because two objects (Date or other) are never == or === to each other, so:

new Date(/* anything */) == new Date(/* anything */)

is always false, even if both Date objects represent invalid dates. If you want to see if they're equal, compare the time values:

new Date('2022-01-06').getTime() == new Date('2022-01-06').getTime()

But be careful when parsing strings, see Why does Date.parse give incorrect results?

RobG
  • 142,382
  • 31
  • 172
  • 209
-1

Don't ever use string literals for dates (especially in JavaScript where everything is a duck) for comparison!

Dates and DateTimes are weird and strange and not actually usable in programming, but since we need to do something with it we kinda "jumped around the whole - this is insane - part".

Just for fun you might want to read Jon Skeet's blog post about it: https://codeblog.jonskeet.uk/2012/05/02/more-fun-with-datetime/

But just for the sake of argument, compare a date with a literal date will resolve in '2021-02-23' < 'AA' which is also true or false (didn't try it), but for JavaScript the comparison is alike, there is no type comparison, it's just values.

riffnl
  • 3,248
  • 1
  • 19
  • 32
  • "*Don't ever use string literals for dates*" is not reasonable advice. One of the benefits of ISO 8601 formatting is that dates compare correctly as strings. Also "*for JavaScript … there is no type comparison*" ignores that the OP is comparing strings, so there is no issue with type and there is `===` (hence `'2020-11-21' === '2020-11-21'` returns *true*). – RobG Jan 06 '22 at 07:51