Rust Formatting: Date/Time
Tags: react, datetime, format
Published: 2023-01-14 15:27:04

In this blog post, let us see how to format Date/Time using different specifiers

##### Formatting Date

###### Format date in `mm/dd/yy` format

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in mm/dd/yy format: {}", dt.format("%D"));
```

###### Format date in `yyyy-mm-dd` format

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in yyyy-mm-dd format: {}", dt.format("%F"));
```

###### Format date in `d-MMM-yyyy` format

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in d-MMM-yyyy format: {}", dt.format("%v"));
```

###### Format date in `Day, dd-mm-yyyy` format

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in Day, dd-mm-yyyy format: {}", dt.format("%a, %d-%M-%Y"));
```

###### Format date in `Day, dd-mm-yyyy` format

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in Day, dd-mm-yyyy format: {}", dt.format("%A, %d-%M-%Y"));
```

###### Format date in `Full Week Day, dd-MMM-yyyy` format

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in Full Week Day, dd-MMM-yyyy format: {}", dt.format("%A, %d-%b-%Y"));
```

###### Format date in `Full Week Day, dd-MMMM-yyyy` format

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date in Full Week Day, dd-MMMM-yyyy format: {}", dt.format("%A, %d-%B-%Y"));
```

###### Retrieve day of the year from Date

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Day of the year: {}", dt.format("%j"));
```

###### Retrieve week day from Date

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Day of the year: {}, Week day (Starting with Sunday): {}", dt.format("%j"), dt.format("%u"));
    println!("Day of the year: {}, Week day (Starting with Monday): {}", dt.format("%j"), dt.format("%w"));
```

##### Formatting Time

###### Time in 24-hour format HH:mm:ss

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Time in 24-hour format HH:mm:ss: {}", dt.format("%H:%M:%S"));
```

###### Time in 12-hour format HH:mm:ss

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Time in 12-hour format HH:mm:ss: {}", dt.format("%I:%M:%S"));
```

###### Time in 12-hour format HH:mm:ss

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Time in 12-hour format HH:mm:ss <AM/PM>: {}", dt.format("%I:%M:%S %p"));
```

###### Time in 12-hour format HH:mm:ss with timezone

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Time in 12-hour format HH:mm:ss <AM/PM> TZ: {}", dt.format("%I:%M:%S %p %:z"));
```

###### Date and Time in ISO 8601/RFC 3339 format

```rust
    let curr_time = SystemTime::now();
    let dt: DateTime<Utc> = curr_time.clone().into();
    println!("Date and Time: {}", dt.format("%c"));
    println!("Date and Time in ISO 8601/RFC 3339 format: {}", dt.format("%+"));
```

All the code examples can be found in this [link](https://github.com/sivakumar-sivaprakasam/rust-examples/tree/main/datetime)

Please feel free to share your feedback.

Happy reading!!!