The words Under construction in black text on a yellow background with diagonal black stipes surrounding it
I'm in the process of moving my site. It's still a work in progress. Please excuse the mess and broken links.

Rust DateTime Format With Abbreviated Months In chrono

TODO: Pull subtitle into page object
Code
//! ```cargo
//! [dependencies]
//! chrono = "0.4.31"
//! ```

use chrono::prelude::*;

fn main() {
  let date_string = "2003-01-08 04:05:06";
  let dt = NaiveDateTime::parse_from_str(date_string, "%Y-%m-%d %H:%M:%S").unwrap();
  println!("{}", abbreviated_month_string(dt));

  let date_string = "2003-05-08 04:05:06";
  let dt = NaiveDateTime::parse_from_str(date_string, "%Y-%m-%d %H:%M:%S").unwrap();
  println!("{}", abbreviated_month_string(dt));
}

fn abbreviated_month_string(dt: NaiveDateTime) -> String {
  match dt.format("%b").to_string().as_ref() {
    "May" => dt.format("%b %Y").to_string(),
    _ => dt.format("%b. %Y").to_string()
  }
}
Results
Jan. 2003
May 2003

Debugging Stuff

I'm moving stuff around right now. All this below is helping me figure out where to put stuff

        -- title

Rust DateTime Format With Abbreviated Months In chrono




-- code
-- rust

//! ```cargo
//! [dependencies]
//! chrono = "0.4.31"
//! ```

use chrono::prelude::*;

fn main() {
  let date_string = "2003-01-08 04:05:06";
  let dt = NaiveDateTime::parse_from_str(date_string, "%Y-%m-%d %H:%M:%S").unwrap();
  println!("{}", abbreviated_month_string(dt));

  let date_string = "2003-05-08 04:05:06";
  let dt = NaiveDateTime::parse_from_str(date_string, "%Y-%m-%d %H:%M:%S").unwrap();
  println!("{}", abbreviated_month_string(dt));
}

fn abbreviated_month_string(dt: NaiveDateTime) -> String {
  match dt.format("%b").to_string().as_ref() {
    "May" => dt.format("%b %Y").to_string(),
    _ => dt.format("%b. %Y").to_string()
  }
}

-- results/

Jan. 2003
May 2003

-- /results


-- categories
-- Rust 

-- metadata
-- date: 2023-12-21 17:50:15
-- id: 2zs7cumq
-- site: aws
-- type: post
-- status: published