-5

After a while of fiddling, I decided to ask the question here, because - others shallst not waste as much fiddling time as I have.

So, how to convert a DateTime<Utc> into a NaiveDate with Rusts chrono crate?

Here the "fill in the blanks" kind of test code:

#[test]
  fn test_utc_now_to_naive_date() {
    let utc_now = Utc::now();
    let now: NaiveDate = ???? // how?
  }

I attribute the fact, that in most languages, time and date function libraries are over- designed monsters to the PTSD people suffered from the Y2K bug...

We have traits like Datelike and should that not help converting one date-like thing into another? Well - I could not find the solution...

cafce25
  • 15,907
  • 4
  • 25
  • 31
BitTickler
  • 10,905
  • 5
  • 32
  • 53

1 Answers1

4

The methods DateTime.naive_utc and NaiveDateTime.date are well documented:

let now: NaiveDate = utc_now.naive_utc().date();

Or the even simpler version from Jonas' comment, using DateTime.date_naive:

let now = utc_now.date_naive();
E_net4
  • 27,810
  • 13
  • 101
  • 139
cafce25
  • 15,907
  • 4
  • 25
  • 31