Rust Program to Parse the Date and Time From a String

You are currently viewing Rust Program to Parse the Date and Time From a String

Rust program to parse the date and time from a string

// Rust program to parse date and time 
// from the string

use chrono::{DateTime, NaiveDate, NaiveDateTime, NaiveTime};
use chrono::format::ParseError;

fn main() -> Result<(), ParseError> {
	let date_time = NaiveDateTime::parse_from_str("2021-10-29 13:46:24", "%Y-%m-%d %H:%M:%S")?;
    println!("Parsed date and time is: {}", date_time);
   
    Ok(())
}

Output

$datetime> cargo run
   Compiling datetime v0.1.0 (/home/arvind/Desktop/rust/datetime)
    Finished dev [unoptimized + debuginfo] target(s) in 0.27s
     Running `target/debug/datetime`

Parsed date and time is: 2021-10-29 13:46:24

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply