英文:
How do I get the current date in typst?
问题
I would like to insert the current date in the title section of my document (a-la LaTeX). Is it even possible to do so yet? If so, how?
我想在文档的标题部分插入当前日期(类似于LaTeX)。这是否已经可以实现?如果可以,如何实现?
As an aside, I figure that if typst is truly meant to become a LaTeX successor/alternative, we had better get started on matching all of the incredibly helpful Stack Exchange Q&A!
另外,我认为如果Typst真的打算成为LaTeX的继承者/替代品,我们最好开始匹配所有非常有帮助的Stack Exchange问答!
英文:
I would like to insert the current date in the title section of my document (a-la LaTeX). Is it even possible to do so yet? If so, how?
As an aside, I figure that if typst is truly meant to become a LaTeX successor/alternative, we had better get started on matching all of the incredibly helpful Stack Exchange Q&A!
答案1
得分: 4
目前还不可能。不过,有一个开放的拉取请求旨在添加该功能。
编辑:现在可以了。请参阅@pgsuper的回答。
英文:
It's currently not possible. However, there is an open Pull Request that aims to add that functionality.
EDIT: It is now possible. See @pgsuper's answer.
答案2
得分: 1
以下是已翻译好的内容:
"从Typst v0.5.0开始,可以获取今天的日期;只需使用 datetime.today() 函数,它返回一个包含当前日期、月份和年份的 datetime 对象(参考链接:https://typst.app/docs/reference/construct/datetime/#datetime-today)。请注意,在Typst中,目前无法获取当前的 时间(即小时/分钟/秒),仅能获取当前日期。
您可以将这个日期与 datetime 的 display() 方法结合使用,以以您所需的方式显示日期(参考链接:https://typst.app/docs/reference/types/datetime/#methods-display),或直接使用 .day() / .month() / .year() datetime 方法:
#let today = datetime.today()
#today.display() // 使用默认格式:年-月-日
#today.display("[day]/[month]/[year]") // 指定自定义格式
#today.display("[month repr:long] [day], [year]") // 或其他自定义格式
#today.day() / #today.month() / #today.year() // 您还可以这样做(不带零填充)
// 上述方法直接返回整数,可在脚本中使用
在2023年9月12日,UTC时间(撰写此答案时),Typst上述代码将生成(在Typst v0.7.0上,使用Discord上的渲染机器人):
<details>
<summary>英文:</summary>
It is possible to obtain today's date since Typst v0.5.0; simply use the `datetime.today()` function, which returns a `datetime` object containing the current day, month and year (see here for reference: https://typst.app/docs/reference/construct/datetime/#datetime-today). Note that in Typst there is, currently, no way to get the current _time_ (i.e., hours/minutes/seconds), however (just the current date).
You can combine this with datetime's `display()` method to display the datetime in your desired way (see https://typst.app/docs/reference/types/datetime/#methods-display for reference), or use the `.day()` / `.month()` / `.year()` datetime methods directly:
```js
#let today = datetime.today()
#today.display() // use the default format: year-month-day
#today.display("[day]/[month]/[year]") // specify a custom format
#today.display("[month repr:long] [day], [year]") // or another custom format
#today.day() / #today.month() / #today.year() // you can also do this (not zero-padded)
// the methods above return integers directly which can be used in scripting
On the 12th of September of 2023, UTC (when this answer was written), the Typst code above will produce (on Typst v0.7.0, using the rendering bot on Discord):
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论