hometools

Parse a toml Date with chrono in Rust

April 2026

Overview

This is how I'm parsing dates that come in from TOML with chrono for formatting.

Rust

Cargo.toml
[package]
name = "parse_toml_date"
version = "0.1.0"
edition = "2024"

[dependencies]
anyhow = "1.0.102"
chrono = "0.4.44"
toml = "1.1.2"
src/main.rs
use anyhow::Result;
use chrono::{DateTime, Local};
use toml::Table;

fn main() -> Result<()> {
  let input = "date = 2026-04-06T14:23:43-04:00";
  let toml = &input.parse::<Table>()?;
  let key = "date";
  let format = "%a. %B %-d - %-I:%M%p";
  if let Some(date) = get_date(toml, key) {
    println!("{}", date.format(format));
  } else {
    println!("Could not get date");
  }
  Ok(())
}

fn get_date(
  toml: &Table,
  key: &str,
) -> Option<DateTime<Local>> {
  match toml.get(key) {
    Some(dt) => dt.to_string().parse::<DateTime<Local>>().ok(),
    None => None,
  }
}

Output

Mon. April 6 - 2:23PM