Rust:
Getting the current date

How to:

Using Rust’s Standard Library

Rust’s standard library provides a limited but quick way to get the current time, though not directly the current date in a calendar format. Here’s how you do it:

use std::time::{SystemTime, UNIX_EPOCH};

fn main() {
    match SystemTime::now().duration_since(UNIX_EPOCH) {
        Ok(n) => println!("Current time: {} seconds since the Unix Epoch.", n.as_secs()),
        Err(_) => panic!("SystemTime before Unix Epoch!"),
    }
}

Output:

Current time: 1615390665 seconds since the Unix Epoch.

Using the Chrono Library

For more comprehensive date and time functionality, including getting the current date, you should use the chrono library. First, add chrono to your Cargo.toml:

[dependencies]
chrono = "0.4"

Then, you can use chrono to get the current date:

extern crate chrono;
use chrono::{Local, Datelike};

fn main() {
    let now = Local::now();
    println!("Current date: {}-{}-{}", now.year(), now.month(), now.day());
}

Output:

Current date: 2023-4-20

The chrono library makes it straightforward to work with dates and times, offering a wide range of functionalities beyond just retrieving the current date, including parsing, formatting, and arithmetic operations on dates and times.