Compare chrono dates



examples/chrono/chrono-compare-dates/Cargo.toml
[package]
name = "chrono-compare-dates"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
chrono = "0.4"

examples/chrono/chrono-compare-dates/src/main.rs
use chrono::{DateTime, Duration, Utc};

fn main() {
    let now: DateTime<Utc> = Utc::now();
    println!("{}", now);

    let later = now + Duration::seconds(1);
    println!("{}", later);

    println!("later is bigger: {}", now < later);
    println!("later is not smaller: {}", now > later);
    println!("later is not the same as now: {}", now == later);
    println!();
    println!("{:?}", now.cmp(&later));
    println!();

    let now2 = later - Duration::seconds(1);
    println!("{}", now2);
    println!("now is now: {}", now == now2);
}

2023-10-20 11:54:30.285843755 UTC
2023-10-20 11:54:31.285843755 UTC
later is bigger: true
later is not smaller: false
later is not the same as now: false

Less

2023-10-20 11:54:30.285843755 UTC
now is now: true