没有jodatime,rust怎么方便高效的操作时间呢?

关注我,学习Rust不迷路!!

当使用Rust进行日期操作时,可以使用 chrono 库。下面给出了二十个常见的日期操作的例子:

1. 获取当前日期和时间:

bash 复制代码
use chrono::prelude::*;

let current_datetime = Local::now();
println!("Current datetime: {}", current_datetime);

2. 获取当前日期:

bash 复制代码
use chrono::prelude::*;

let current_date = Local::today();
println!("Current date: {}", current_date);

3. 获取当前时间:

bash 复制代码
use chrono::prelude::*;

let current_time = Local::now().time();
println!("Current time: {}", current_time);

4. 根据年、月、日创建日期:

bash 复制代码
use chrono::{NaiveDate, Local};

let date = NaiveDate::from_ymd(2022, 1, 1);
println!("Date: {}", date);

5. 根据时、分、秒创建时间:

bash 复制代码
use chrono::{NaiveTime, Local};

let time = NaiveTime::from_hms(12, 34, 56);
println!("Time: {}", time);

6. 将日期和时间合并为日期时间:

bash 复制代码
use chrono::{NaiveDate, NaiveTime, NaiveDateTime};

let date = NaiveDate::from_ymd(2022, 1, 1);
let time = NaiveTime::from_hms(12, 34, 56);
let datetime = NaiveDateTime::new(date, time);
println!("Datetime: {}", datetime);

7. 格式化日期时间为字符串:

bash 复制代码
use chrono::{NaiveDateTime, format::strftime};

let datetime = NaiveDateTime::parse_from_str("2022-01-01 12:34:56", "%Y-%m-%d %H:%M:%S").unwrap();
let formatted_datetime = datetime.format("%Y-%m-%d %H:%M:%S").to_string();
println!("Formatted datetime: {}", formatted_datetime);

8. 在日期上进行加减操作:

bash 复制代码
use chrono::{NaiveDate, Duration};

let date = NaiveDate::from_ymd(2022, 1, 1);
let new_date = date + Duration::days(7);
println!("New date: {}", new_date);

9. 计算两个日期之间的天数差:

bash 复制代码
use chrono::{NaiveDate, Duration};

let date1 = NaiveDate::from_ymd(2022, 1, 1);
let date2 = NaiveDate::from_ymd(2022, 1, 10);
let duration = date2.signed_duration_since(date1);
let days_diff = duration.num_days();
println!("Days difference: {}", days_diff);

10. 比较两个日期的先后:

bash 复制代码
use chrono::{NaiveDate, Ordering};

let date1 = NaiveDate::from_ymd(2022, 1, 1);
let date2 = NaiveDate::from_ymd(2022, 1, 10);
let ordering = date1.cmp(&date2);
println!("Date1 is {:?} Date2", ordering);

11. 获取当前月份:

bash 复制代码
use chrono::prelude::*;

let current_month = Local::today().month();
println!("Current month: {}", current_month);

12. 获取当前年份:

bash 复制代码
use chrono::prelude::*;

let current_year = Local::today().year();
println!("Current year: {}", current_year);

13. 获取当前日期是星期几:

bash 复制代码
use chrono::prelude::*;

let current_weekday = Local::today().weekday();
println!("Current weekday: {}", current_weekday);

14. 获取指定日期的年份:

bash 复制代码
use chrono::prelude::*;

let date = NaiveDate::from_ymd(2022, 1, 1);
let year = date.year();
println!("Year: {}", year);

15. 获取指定日期的月份:

bash 复制代码
use chrono::prelude::*;

let date = NaiveDate::from_ymd(2022, 1, 1);
let month = date.month();
println!("Month: {}", month);

16. 获取指定日期的日:

bash 复制代码
use chrono::prelude::*;

let date = NaiveDate::from_ymd(2022, 1, 1);
let day = date.day();
println!("Day: {}", day);

17. 获取指定日期是星期几:

bash 复制代码
use chrono::prelude::*;

let date = NaiveDate::from_ymd(2022, 1, 1);
let weekday = date.weekday();
println!("Weekday: {}", weekday);

18. 获取指定日期所在月份的天数:

bash 复制代码
use chrono::prelude::*;

let date = NaiveDate::from_ymd(2022, 2, 1);
let days_in_month = date.with_day0(0).unwrap().day();
println!("Days in month: {}", days_in_month);

19. 根据日期计算年龄:

bash 复制代码
use chrono::prelude::*;

let birthday = NaiveDate::from_ymd(1990, 5, 15);
let today = Local::today().naive_local();
let age = today.year() - birthday.year();
println!("Age: {}", age);

20. 判断是否为闰年:

bash 复制代码
use chrono::prelude::*;

let year = 2024;
let is_leap_year = NaiveDate::from_ymd_opt(year, 2, 29).is_some();
println!("Is leap year: {}", is_leap_year);

这些例子展示了如何使用 chrono 库进行日期操作,包括获取当前日期和时间、创建日期和时间、格式化日期和时间、进行加减操作、计算差值、比较日期的先后顺序等。

关注我,学习Rust不迷路!!

相关推荐
远望清一色15 分钟前
基于MATLAB的实现垃圾分类Matlab源码
开发语言·matlab
confiself24 分钟前
大模型系列——LLAMA-O1 复刻代码解读
java·开发语言
XiaoLeisj36 分钟前
【JavaEE初阶 — 多线程】Thread类的方法&线程生命周期
java·开发语言·java-ee
杜杜的man39 分钟前
【go从零单排】go中的结构体struct和method
开发语言·后端·golang
幼儿园老大*40 分钟前
走进 Go 语言基础语法
开发语言·后端·学习·golang·go
半桶水专家41 分钟前
go语言中package详解
开发语言·golang·xcode
llllinuuu42 分钟前
Go语言结构体、方法与接口
开发语言·后端·golang
cookies_s_s42 分钟前
Golang--协程和管道
开发语言·后端·golang
王大锤439144 分钟前
golang通用后台管理系统07(后台与若依前端对接)
开发语言·前端·golang
为什么这亚子1 小时前
九、Go语言快速入门之map
运维·开发语言·后端·算法·云原生·golang·云计算