37

I have two luxon objects,

let startDate = DateTime.fromISO(startDate)
let someDate = DateTime.fromISO(someDate)

How can I compare if someDate is <= startDate, only the dates, without the time?

Petran
  • 7,677
  • 22
  • 65
  • 104
  • 1
    Do you have issue using `<` and `>`? From luxon [Comparing DateTimes](https://moment.github.io/luxon/docs/manual/math.html#comparing-datetimes) section of the manual: _DateTime implements `#valueOf` to return the epoch timestamp, so you can compare DateTimes with `<`, `>`, `<=`, and `>=`. That lets you find out if one DateTime is after or before another DateTime._ – VincenzoC Feb 04 '20 at 13:50
  • Sorry, I have updated my question – Petran Feb 04 '20 at 15:53
  • Which are the values of `startDate` and `someDate`? Maybe you can use `startOf`, but I think that no matter the time, if a date is previous another one you will get the right result using `<`, `>`, `<=` and `>=` – VincenzoC Feb 04 '20 at 16:03

6 Answers6

68

To compare just the dates, use startOf

startDate.startOf("day") <= someDate.startOf("day")
snickersnack
  • 2,087
  • 11
  • 17
17

Documentation: https://moment.github.io/luxon/#/math?id=comparing-datetimes

var DateTime = luxon.DateTime;

var d1 = DateTime.fromISO('2017-04-30');
var d2 = DateTime.fromISO('2017-04-01');

console.log(d2 < d1); //=> true
console.log(d2 > d1); //=> false
<script src="https://moment.github.io/luxon/global/luxon.min.js"></script>
xinthose
  • 3,213
  • 3
  • 40
  • 59
  • 1
    This does not address comparison of only Date components of the DateTime objects. The example in this answer will still compare time components if they are present. – Mark Clark May 09 '23 at 19:57
  • @MarkClark that is why we use luxon instead – xinthose May 10 '23 at 14:22
  • I'm not talking about JS dates; `DateTime` _is_ luxon. I don't understand what you are getting at. – Mark Clark May 11 '23 at 15:53
  • @MarkClark I guess I did not know `DateTime` has a `Date` component. I thought you were referring to a JavaScript `Date`. – xinthose May 11 '23 at 16:53
  • It doesn't. I am referring to "date component" as a concept. Luxon's `DateTime` is three concepts rolled into one object, date, time, and timezone. When using comparison operators (>,<,=), it will compare those objects with respect to all three concepts, irrelevant of how they were initialized (as in your example with only date strings). The original question explicitly asks about comparing only the date concept of two arbitrary luxon DateTimes. Hence my initial protest to this answer. – Mark Clark May 12 '23 at 15:31
2

As an additional option, if your input strings are known to be ISO 8601 strings as hinted in your question, it's valid to compare them lexicographically:

"2007-01-17T08:00:00Z" < "2008-01-17T08:00:00Z" === true;
"2007-01-17" < "2008-01-17" === true;
Etheryte
  • 24,589
  • 11
  • 71
  • 116
-1

Another option presented by this github issue could be to create your own Date class:

Luxon doesn’t have any support for [separate Date classes] but it’s easy to do through composition: create a Date class that wraps DateTime and exposes the subset of methods you need. You can decide how pure of abstraction you need to provide to your application (ie how much work you want to do to make your wrapper full featured and pure)

MoralCode
  • 1,954
  • 1
  • 20
  • 42
-5

Use ordinal to get the day of the year as an integer, see https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html#instance-get-ordinal

startDate.ordinal <= someDate.ordinal
Marcelo Diniz
  • 2,493
  • 2
  • 21
  • 27
  • The accepted answer did not work for me, but this did. Thanks! – Zacho Aug 05 '20 at 17:46
  • 29
    This is a somewhat dangerous answer; if the startDate is a different year than someDate, all bets are off. It will think than Jan 1, 2021 is before Dec 31, 2020, for example. – snickersnack Sep 04 '20 at 06:20
-5

Neither of these answers work with leap years. I found success with the following,

function dateTimesAreSameDay(dateTime1, dateTime2) {
  return dateTime1.year === dateTime2.year && dateTime1.month === dateTime2.month && dateTime1.day === dateTime2.day;
}
Aaron Mast
  • 249
  • 3
  • 4