26

Luxon's documentation for the Duration.fromISO method describes it as

Create a Duration from an ISO 8601 duration string

Nowhere is mentioned the ability to create a duration based on two dates. My typical use case would be: "did the event between date ISODAT1 and ISODATE2 last more than an hour?".

What I will do is to transform the dates into a timestamp and check whether the difference is greater than 3600 (seconds), I believe however that there is a more native way to make the check.

43

You could use DateTime's .diff (doc)

Return the difference between two DateTimes as a Duration.

const date1 = luxon.DateTime.fromISO("2020-09-06T12:00")
const date2 = luxon.DateTime.fromISO("2019-06-10T14:00")

const diff = date1.diff(date2, ["years", "months", "days", "hours"])

console.log(diff.toObject())
<script src="https://cdn.jsdelivr.net/npm/luxon@1.25.0/build/global/luxon.min.js"></script>

2
4

Example

const date1 = luxon.DateTime.fromISO("2020-09-06T12:00");
const date2 = luxon.DateTime.fromISO("2019-06-10T14:00");
const diff = Interval.fromDateTimes(later, now);
const diffHours = diff.length('hours');

if (diffHours > 1) {
  // ...
}

Documentation for Luxon v2.x

In the Luxon documentation they mention durations and intervals. It seems that you would be best off using Intervals and then calling the .length('hours') on the Interval if you're interested to know if something has been over an hour.

Durations

The Duration class represents a quantity of time such as "2 hours and 7 minutes".

const dur = Duration.fromObject({ hours: 2, minutes: 7 });

dur.hours;   //=> 2
dur.minutes; //=> 7
dur.seconds; //=> 0

dur.as('seconds'); //=> 7620
dur.toObject();    //=> { hours: 2, minutes: 7 }
dur.toISO();       //=> 'PT2H7M'

Intervals

Intervals are a specific period of time, such as "between now and midnight". They're really a wrapper for two DateTimes that form its endpoints.

const now = DateTime.now();
const later = DateTime.local(2020, 10, 12);
const i = Interval.fromDateTimes(now, later);

i.length()                             //=> 97098768468
i.length('years')                      //=> 3.0762420239726027
i.contains(DateTime.local(2019))       //=> true

i.toISO()       //=> '2017-09-14T04:07:11.532-04:00/2020-10-12T00:00:00.000-04:00'
i.toString()    //=> '[2017-09-14T04:07:11.532-04:00 – 2020-10-12T00:00:00.000-04:00)
1
  • The irony is that on the project that got me looking into this my teammate ended up recommending we go with something like: DateTime.now().diff(DateTime.fromISO(date))
    – CTS_AE
    Sep 16, 2021 at 16:46

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.