THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.
This program prints the current date and time in the system and UTC timezone.
It uses the Chrono library to access current timestamp-related information.
Let’s create a new rust project using cargo command.
cargo new dateapp
Next, Change the directory to dateapp
cd dateapp
Add the Chrono dependency in cargo.toml file
[dependencies]
chrono = "0.4"
Now, Run the cargo build command to install all dependencies.
cargo build
Let’s write a code for the printing date in local and UTC format.
Utc::now()
: returns DateTime<Utc>
object that contains UTC date and time
Local::now()
: returns DateTime<Local>
object that contains System date and time
Here is an example program
use chrono::{DateTime, Local, Utc};
fn main() {
let utc: DateTime = Utc::now();
let local: DateTime = Local::now();
println!("Current Date and Time in UTC {:?}", utc);
println!("Current Date and Time in System {:?}", utc);
}
Running the above code using cargo run
.
PS A:\work\rust\dateapp> cargo run
Compiling dateapp v0.1.0 (A:\work\rust\dateapp)
Finished dev [unoptimized + debuginfo] target(s) in 1.56s
Running `target\debug\dateapp.exe`
Current Date and Time in UTC 2022-04-29T10:53:01.715904300Z
Current Date and Time in System 2022-04-29T16:23:01.715941800+05:30
🧮 Tags
Recent posts
Three dots in react components with example|Spread operator How to update ReactJS's `create-react-app`? Difference between super and super props in React constructor class? How to check the element index of a list or array in the swift example How to calculate the sum of all numbers or object values in Array swift with exampleRelated posts