0

I got the following property in an object, it is a string:

{end_date: '2017-04-05'}

I also have this :

const date = new Date();

How can i check if the end_date property is in the past. Something like

(obj.end_date < date ? true : false)
Kevin.a
  • 4,094
  • 8
  • 46
  • 82
  • 1
    Does this answer your question? [Compare two dates with JavaScript](https://stackoverflow.com/questions/492994/compare-two-dates-with-javascript) – JSON Derulo Apr 28 '20 at 13:16
  • What timezone does the string in `end_date` represent? It'll matter to the correctness of the solution. – T.J. Crowder Apr 28 '20 at 13:17
  • Does this answer your question? [Converting a string to a date in JavaScript](https://stackoverflow.com/questions/5619202/converting-a-string-to-a-date-in-javascript) – AZ_ Apr 28 '20 at 13:17

2 Answers2

4

If end_date is in UTC, you can do this:

const endDate = new Date(obj.end_date + "Z");
const inThePast = endDate < date;

The Z says UTC.

If it's local time (yes, it matters even with just a date), your best bet is to parse it and build it from parts:

const [year, month, day] = obj.end_date.split("-");
const endDate = new Date(+year, month - 1, +day); // Local time
const inThePast = endDate < date;

Without a timezone indicator on it, the spec says it should be parsed as UTC, hence creating it as above. However, this varied a bit in the specification, and implementations varied with it. ES5 said all strings without timezone indicators should be parsed as UTC, which was incompatible with ISO-8601; ES2015 said the opposite, which turned out to be incompatible with a lot of existing code; ES2016 and onward say UTC for date-only forms and local time for date/time forms). As of the end of 2018, my answer here says that with reasonably-modern browsers, the date-only form should reliably be parsed as UTC cross-browser. But if you want local, you have to do something else.

Non-standard, you could also rearrange it to the U.S. date format MM/DD/YYYY (with /, not -) and every browser I've tried would parse it as local time. But if you're going to do that, you may as well use the constructor as shown above to specify the parts explicitly.

T.J. Crowder
  • 1,031,962
  • 187
  • 1,923
  • 1,875
  • why not `new Date(string without Z)` for local? wouldn't it take local timezone? – AZ_ Apr 28 '20 at 13:25
  • @AZ_ - Without a timezone indicator, the date-only form is supposed to be parsed as UTC, but see the updated answer -- that part of the specification had to move around a bit. :-) – T.J. Crowder Apr 28 '20 at 13:44
  • Timezone is CEST , but i can work with this making some adjusments. Thank you – Kevin.a Apr 28 '20 at 13:56
  • 1
    Love that de-structuring trick with year month and day. Nice one! – Terry Lennox Apr 29 '20 at 11:12
  • 1
    @TerryLennox - Thanks! Except it was wrong. :-) When I wrote `const [, year, month, day] =` I was thinking I'd use `obj.end_date.match(...)` with a regular expression and capture groups (hence skipping the first entry), then after the `=` I used `split` instead, and never went back and removed the leading `,`. Fixed now. :-) – T.J. Crowder Apr 29 '20 at 12:17
  • Ah yes, ok. It's a nice pattern, though, I'll be using it! – Terry Lennox Apr 29 '20 at 12:20
1

If you do not need to check timezone this should be enought:

const endDate = new Date(obj.end_date);
const isPast = endDate < date
Adam Orłowski
  • 4,268
  • 24
  • 33
  • Sadly, without a timezone indicator on it you can't be sure whether that date will be parsed as UTC or local time because, sadly, the spec has varied and last I checked (a few months ago) implementations still varied. So this seemingly-simple solution may be incorrect...sometimes...making for hard-to-debug issues. – T.J. Crowder Apr 28 '20 at 13:20
  • @T.J.Crowder you are right. I have added info about timezone to my answer. I have spend a lot of time one day debugging timezones. Thanks. – Adam Orłowski Apr 28 '20 at 13:21