Function ui2::utils::naive_format_distance_from_now
source · pub fn naive_format_distance_from_now(
datetime: NaiveDateTime,
include_seconds: bool,
add_suffix: bool
) -> String
Expand description
Get the time difference between a date and now as relative human readable string.
For example, “less than a minute ago”, “about 2 hours ago”, “3 months from now”, etc.
Arguments
datetime
- The NaiveDateTime to compare with the current time.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_from_now;
fn time_since_first_moon_landing() -> String {
let date = DateTime::parse_from_rfc3339("1969-07-20T00:00:00Z").unwrap().naive_local();
format!("It's been {} since Apollo 11 first landed on the moon.", naive_format_distance_from_now(date, false, false))
}
Output: It's been over 54 years since Apollo 11 first landed on the moon.