11

I am trying to calculate the difference between two DateTime instances using clj-time. I looked through the documentation and found clj-time.core/minus and clj-time.core/minus- but both require an instance of org.joda.time.ReadablePeriod and not one of DateTime. I would prefer to use cli-time, but if no solution exists, what would be the cleanest method of calculating the delta using Joda Time?

Thank you for the help.

davenportw15
  • 157
  • 2
  • 10

1 Answers1

14

I use this snippet very often in my daily work:

(ns project.namespace1
  (:require [clj-time
              [core :as t]] )

(let [start-time (t/now)]
  ... do lots of work ...
  (t/in-millis (t/interval start-time (t/now))))
Arthur Ulfeldt
  • 90,827
  • 27
  • 201
  • 284
  • That works well. Thank you. I have one final question, however: is there no way to return an instance on which one can call `t/hour`, for example? Or do we have to do math with the milliseconds ourselves? – davenportw15 Feb 06 '15 at 04:20
  • 1
    There's also `in-hours`, `in-days` and so on. Remember that `hour` is for querying "What hour of the day is this event?". An interval doesn't happen at a particular time of the day, so this question doesn't make any sense to ask. – Magos Feb 06 '15 at 05:09
  • So math it is then. Thanks for the help. – davenportw15 Feb 06 '15 at 18:19