Function ui2::utils::naive_format_distance
source · pub fn naive_format_distance(
date: NaiveDateTime,
base_date: NaiveDateTime,
include_seconds: bool,
add_suffix: bool
) -> String
Expand description
Get the time difference between two dates into a relative human readable string.
For example, “less than a minute ago”, “about 2 hours ago”, “3 months from now”, etc.
Use naive_format_distance_from_now to compare a NaiveDateTime against now.
Arguments
date
- The NaiveDateTime to compare.base_date
- The NaiveDateTime to compare against.include_seconds
- A boolean. If true, distances less than a minute are more detailedadd_suffix
- A boolean. If true, result indicates if the time is in the past or future
Example
use chrono::DateTime;
use ui2::utils::naive_format_distance;
fn time_between_moon_landings() -> String {
let date = DateTime::parse_from_rfc3339("1969-07-20T00:00:00Z").unwrap().naive_local();
let base_date = DateTime::parse_from_rfc3339("1972-12-14T00:00:00Z").unwrap().naive_local();
format!("There was {} between the first and last crewed moon landings.", naive_format_distance(date, base_date, false, false))
}
Output: "There was about 3 years between the first and last crewed moon landings."