简体   繁体   English

如何使用 Chrono crate 在 Rust 中获取当前工作日?

[英]How can I get the current weekday in Rust using the Chrono crate?

I am trying to get the current weekday in Rust using the Chrono crate.我正在尝试使用 Chrono 板条箱在 Rust 中获取当前工作日。

The JavaScript equivalent would be something like this: JavaScript 等效项是这样的:

new Date().toLocaleDateString('en-US',{weekday: 'long'});

I am getting the current timestamp with the following code:我正在使用以下代码获取当前时间戳:

let current_time = chrono::offset::Local::now();

I tried to call a .weekday() method on the resulting DateTime struct unsuccessfully.我尝试在生成的DateTime结构上调用.weekday()方法失败。 I see the DateLike trait offers something of that nature in the documentation, but I find myself unable to parse the documentation and produce the corresponding code without an example.我看到DateLike特征在文档中提供了这种性质的东西,但我发现自己无法解析文档并在没有示例的情况下生成相应的代码。

The DateLike trait, which is implemented by DateTime contains a common set of methods for date components, including weekday .DateTime实现的DateLike trait 包含一组通用的日期组件方法,包括weekday You can get a DateTime from a Local offset with the date method:您可以使用date方法从Local偏移量获取DateTime

use chrono::Datelike;

let current_time = chrono::offset::Local::now();
println!("{}", current_time.date().weekday());

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM