简体   繁体   English

仅将日期与 luxon Datetime 进行比较

[英]compare only dates with luxon Datetime

I have two luxon objects,我有两个 luxon 对象,

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

How can I compare if someDate is <= startDate, only the dates, without the time?我如何比较 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

Neither of these answers work with leap years.这些答案都不适用于闰年。 I found success with the following,我在以下方面取得了成功,

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

Documentation: https://moment.github.io/luxon/#/math?id=comparing-datetimes文档: 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>

Another option presented by this github issue could be to create your own Date class:这个github issue 提出的另一个选项可能是创建你自己的 Date 类:

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. Luxon 不支持 [单独的 Date 类],但通过组合很容易做到:创建一个 Date 类来包装 DateTime 并公开您需要的方法子集。 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)你可以决定你需要为你的应用程序提供的抽象的纯度(即你想要做多少工作来使你的包装器功能齐全和纯净)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM