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?
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?
To compare just the dates, use startOf
startDate.startOf("day") <= someDate.startOf("day")
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>
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;
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)
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
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;
}