仅将日期与 luxon Datetime 进行比较

compare only dates with luxon Datetime

我有两个 luxon 对象,

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

如果 someDate <= startDate,只有日期,没有时间,我该如何比较?

要仅比较日期,请使用 startOf

startDate.startOf("day") <= someDate.startOf("day")

使用 ordinal 得到一年中的第几天作为整数,参见 https://moment.github.io/luxon/docs/class/src/datetime.js~DateTime.html#instance-get-ordinal

startDate.ordinal <= someDate.ordinal

这些答案都不适用于闰年。我在以下方面取得了成功,

function dateTimesAreSameDay(dateTime1, dateTime2) {
  return dateTime1.month === dateTime2.month && dateTime1.day === dateTime2.day;
}

文档: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>

this github 问题提出的另一个选项可能是创建您自己的日期 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)