2

I am using chrono. I have now() and some other NaiveDateTime. How can I find a difference between them?

let now = Utc::now().naive_utc();
let dt1 = get_my_naive_datetime();
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Kurama
  • 447
  • 1
  • 5
  • 5

1 Answers1

7

In more recent versions of chrono (at least as of 0.4.22, and likely earlier), you can simply subtract NaiveDateTimes:

println!("{:?}", dt1 - now);

The result is a Duration, which has methods to convert to whatever units you like, e.g. (dt1 - now).num_days().

In older versions of chrono, you must use NaiveDateTime::signed_duration_since:

println!("{:?}", dt1.signed_duration_since(now));
trent
  • 25,033
  • 7
  • 51
  • 90
  • how to get that in days, hours, minutes? – Kurama Jan 18 '18 at 04:31
  • 2
    @Kurama what's the question about? This method returns the `Duration` object which has all the methods you want, - to get days, hours and minutes. Just open that link and you'll see. – VP. Jan 18 '18 at 07:51
  • Or you can simply use subtraction to get a `Duration` from two `NaiveDateTime` instances: `now-dt1` – Jmb Jan 18 '18 at 13:34
  • @Jmb which will yield another Duration. What will I do with it? – Kurama Jan 18 '18 at 13:55