关注我,学习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
库进行日期操作,包括获取当前日期和时间、创建日期和时间、格式化日期和时间、进行加减操作、计算差值、比较日期的先后顺序等。