compare only dates with luxon Datetime
Asked Answered
V

6

46

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?

Vandal answered 4/2, 2020 at 13:28 Comment(3)
Do you have issue using < and >? From luxon 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.Norikonorina
Sorry, I have updated my questionVandal
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 >=Norikonorina
H
80

To compare just the dates, use startOf

startDate.startOf("day") <= someDate.startOf("day")
Hahnke answered 10/2, 2020 at 9:38 Comment(2)
we can also check equality by using timestamp startDate.startOf('day').ts == someDate.startOf("day").tsAshcan
I think that if you want to check equality it is better to do something like this: startDate.hasSame(someDate, "day")Evora
S
23

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>
Shortsighted answered 9/7, 2021 at 14:38 Comment(6)
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.Reggiereggis
@MarkClark that is why we use luxon insteadShortsighted
I'm not talking about JS dates; DateTime is luxon. I don't understand what you are getting at.Reggiereggis
@MarkClark I guess I did not know DateTime has a Date component. I thought you were referring to a JavaScript Date.Shortsighted
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.Reggiereggis
This did the job thanks @ShortsightedAcre
L
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;
Lassalle answered 5/11, 2022 at 16:7 Comment(0)
H
-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)

Haberman answered 5/9, 2021 at 0:44 Comment(0)
N
-6

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
Note answered 19/5, 2020 at 17:30 Comment(2)
The accepted answer did not work for me, but this did. Thanks!Hoashis
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.Hahnke
H
-6

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;
}
Hyperbole answered 3/8, 2020 at 1:43 Comment(2)
What about January 1st in different years?Drawknife
Great point! Updated to take this into account, not sure how I missed that one.Hyperbole

© 2022 - 2024 — McMap. All rights reserved.