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")
startDate.startOf('day').ts == someDate.startOf("day").ts
–
Ashcan startDate.hasSame(someDate, "day")
–
Evora 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>
DateTime
is luxon. I don't understand what you are getting at. –
Reggiereggis DateTime
has a Date
component. I thought you were referring to a JavaScript Date
. –
Shortsighted 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 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;
}
© 2022 - 2024 — McMap. All rights reserved.
<
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. – NorikonorinastartDate
andsomeDate
? Maybe you can usestartOf
, but I think that no matter the time, if a date is previous another one you will get the right result using<
,>
,<=
and>=
– Norikonorina