# \[SOLVED\] Rules: calculate difference between 2 DateTime in minutes

**URL:** <https://community.openhab.org/t/solved-rules-calculate-difference-between-2-datetime-in-minutes/91553>\
**Category:** Scripts & Rules\
**Created:** [January 28, 2020, 3:03pm UTC](https://community.openhab.org/t/solved-rules-calculate-difference-between-2-datetime-in-minutes/91553 "2020-01-28T15:03:39Z")\
**Posts on this page:** 1\
**Showing post:** 4

<div class="post-metadata">

**Author:** ![5iver](https://community.openhab.org/user_avatar/community.openhab.org/5iver/32/21201_2.png) [@5iver](https://community.openhab.org/u/5iver)\
**Post date:** [January 28, 2020, 11:44pm UTC](https://community.openhab.org/t/solved-rules-calculate-difference-between-2-datetime-in-minutes/91553/4 "2020-01-28T23:44:25Z")

</div>

[ZonedDateTime](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html)… [until](https://docs.oracle.com/javase/8/docs/api/java/time/ZonedDateTime.html#until-java.time.temporal.Temporal-java.time.temporal.TemporalUnit-)… [ChronoUnit.TemporalUnit](https://docs.oracle.com/javase/8/docs/api/java/time/temporal/TemporalUnit.html)…

```auto
    import java.time.temporal.ChronoUnit
    ...
    val date_1 = Virtual_DateTime_1.getStateAs(DateTimeType).zonedDateTime
    val date_2 = Virtual_DateTime_2.getStateAs(DateTimeType).zonedDateTime
    val seconds_between = date_1.until(date_2, ChronoUnit.SECONDS)
    val minutes_between = date_1.until(date_2, ChronoUnit.MINUTES)
    val days_between = date_1.until(date_2, ChronoUnit.DAYS)
    val weeks_between = date_1.until(date_2, ChronoUnit.WEEKS)
    val months_between = date_1.until(date_2, ChronoUnit.MONTHS)
    val years_between = date_1.until(date_2, ChronoUnit.MONTHS)
    logWarn("Rules", "[{}], [{}]\nSeconds [{}]\nMinutes [{}]\nDays [{}]\nWeeks [{}]\nMonths [{}]\nYears [{}]", date_1, date_2, seconds_between, minutes_between, days_between, weeks_between, months_between, years_between)

```

Here’s another way to use ChronoUnit…

> [@Feeling rusty... compare two times?](https://community.openhab.org/t/feeling-rusty-compare-two-times/91086/9):
>
> It depends on what you’ve written your rule in. With scripted automation, Jython and the helper libraries, you can use the core.date.seconds\_between function… from core.log import logging, LOG\_PREFIX#, log\_traceback from core.date import seconds\_between LOG = logging.getLogger("{}.TEST".format(LOG\_PREFIX)) LOG.warn(seconds\_between(items["Virtual\_DateTime\_1"], items["Virtual\_DateTime\_2"])) However, java.time.temporal.ChonoUnit provides something similar… from java.time.temporal.ChronoUnit i…

---

_[View the full topic](https://community.openhab.org/t/solved-rules-calculate-difference-between-2-datetime-in-minutes/91553)._
