Date Time Formatting In Rust With Chrono

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();
  let formats: Vec<(&str, &str)>= vec![
    ("Year (without century, zero padded)", "%y"),
    ("Year (without century, no padding)", "%-y"),
    ("Year (with centry)", "%Y"),
    ("Month (zero padded)", "%m"),
    ("Month (no padding)", "%-m"),
    ("Month Name (full)", "%B"),
    ("Month Name (abbrevaited)", "%b"),
    ("Day of Month (zero padded)", "%d"),
    ("Day of Month (no padding)", "%-d"),
    ("Day of Week Name (full)", "%A"), 
    ("Day of Week Name (abbreviated)", "%a"), 
    ("Day of Week Zero Index Number", "%w"), 
    ("Day of Year (zero padded)", "%j"),
    ("Day of Year (no padding)", "%-j"),
    ("Hour (24hr clock, zero padded)", "%H"),
    ("Hour (24hr clock, no padding)", "%-H"),
    ("Hour (12hr clock, zero padded)", "%I"),
    ("Hour (12hr clock, no padding)", "%-I"),
    ("Minute (zero padded)", "%M"),
    ("Minute (no padding)", "%-M"),
    ("Second (zero padded)", "%S"),
    ("Second (no padding)", "%-S"),
    ("Microsecond (zero padded)", "%f"),
    ("Week number (Sunday based)", "%U"),
    ("Week number (Monday based)", "%W"),
    ("AM/PM", "%p"),
    ("Locale DateTime String", "%c"),
    ("Locale Date String", "%x"),
    ("Locale Time String", "%X"),
    ("Literal %", "%%"),
  ];
  formats.iter().for_each(|format| 
    {
      println!("{}", format!("{:^4} {:<36} | {:^25}", format.1, format.0, dt.format(format.1)));
      println!("");
    }
  );
}

Results

%y  Year (without century, zero padded)  |            03            

%-y  Year (without century, no padding)   |             3            

 %Y  Year (with centry)                   |           2003           

 %m  Month (zero padded)                  |            01            

%-m  Month (no padding)                   |             1            

 %B  Month Name (full)                    |          January         

 %b  Month Name (abbrevaited)             |            Jan           

 %d  Day of Month (zero padded)           |            08            

%-d  Day of Month (no padding)            |             8            

 %A  Day of Week Name (full)              |         Wednesday        

 %a  Day of Week Name (abbreviated)       |            Wed           

 %w  Day of Week Zero Index Number        |             3            

 %j  Day of Year (zero padded)            |            008           

%-j  Day of Year (no padding)             |             8            

 %H  Hour (24hr clock, zero padded)       |            04            

%-H  Hour (24hr clock, no padding)        |             4            

 %I  Hour (12hr clock, zero padded)       |            04            

%-I  Hour (12hr clock, no padding)        |             4            

 %M  Minute (zero padded)                 |            05            

%-M  Minute (no padding)                  |             5            

 %S  Second (zero padded)                 |            06            

%-S  Second (no padding)                  |             6            

 %f  Microsecond (zero padded)            |         000000000        

 %U  Week number (Sunday based)           |            01            

 %W  Week number (Monday based)           |            01            

 %p  AM/PM                                |            AM            

 %c  Locale DateTime String               | Wed Jan  8 04:05:06 2003 

 %x  Locale Date String                   |         01/08/03         

 %X  Locale Time String                   |         04:05:06         

 %%  Literal %                            |             %