javascript - Determine if a specific DateTime is within a time interval using Luxon - Stack Overflow

I'm trying to figure out how to check if a specific DateTime is within a time interval.For examp

I'm trying to figure out how to check if a specific DateTime is within a time interval. For example, let's say I have a variable named dateTimeToCheck and I want to see if it is within the first quarter of the year (Jan 1st to March 31st).

I could use diff between dateTimeToCheck and Jan 1st and then diff between dateTimeToCheck and March 31st followed by some calculations, but I feel like there should be a cleaner way using Luxon's Interval or something like that.

Any help is very much appreciated!

I'm trying to figure out how to check if a specific DateTime is within a time interval. For example, let's say I have a variable named dateTimeToCheck and I want to see if it is within the first quarter of the year (Jan 1st to March 31st).

I could use diff between dateTimeToCheck and Jan 1st and then diff between dateTimeToCheck and March 31st followed by some calculations, but I feel like there should be a cleaner way using Luxon's Interval or something like that.

Any help is very much appreciated!

Share Improve this question asked Jan 30, 2023 at 22:41 risingTiderisingTide 1,8969 gold badges38 silver badges65 bronze badges
Add a ment  | 

1 Answer 1

Reset to default 12

Yes, you can use Luxon's Interval. The API has the contains method that:

Return whether this Interval contains the specified DateTime.

Here an example where I've used fromDateTimes to create Interval object:

const DateTime = luxon.DateTime;
const Interval = luxon.Interval;

const interval = Interval.fromDateTimes(DateTime.local(2023, 1, 1), DateTime.local(2023, 3, 31));
let dateTimeToCheck = DateTime.now();
console.log(interval.contains(dateTimeToCheck));
<script src="https://cdn.jsdelivr/npm/[email protected]/build/global/luxon.min.js"></script>

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1743768681a4503971.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信