如何在hugo post中呈现正确的日期时间?

问题描述 投票:0回答:5

我使用hugo创建了一个帖子

new posts/mypost.md
,它为我创建了mypost.md,并带有这样的标题toml配置

toml config

但是,当我在服务器(本地)上运行时,日期时间呈现错误,例如: enter image description here

我该如何解决这个问题? 预先感谢!

blogs hugo toml
5个回答
9
投票

这就是我让它工作的方法:

config.toml

添加您的日期格式
[params]
    dateFormat = "02 Jan 2006"

您的帖子应在其标题中包含日期:

---
date: "2020-12-23T15:21:54+05:30"
...
---

在布局中使用格式:

<div>{{ .Params.date.Format .Site.Params.dateFormat }}</div>

注意:请勿更改日期格式中的数字。日必须为 02,月份必须为 Jan,年份必须为 2006,等等。


5
投票

另请检查Hugo 0.87(2021 年 8 月,两年后),其中包含:

请注意,自 Hugo 0.87.0 起,

time.Format
将返回当前语言的本地化字符串。

  • 日期/时间格式布局

    {{ .Date | time.Format ":date_long" }}
    

自定义布局的完整列表以及英语示例:

:date_full => Wednesday, June 6, 2018

:date_long => June 6, 2018

:date_medium => Jun 6, 2018

:date_short => 6/6/18

:time_full => 2:09:37 am UTC

:time_long => 2:09:37 am UTC

:time_medium => 2:09:37 am

:time_short => 2:09 am

3
投票

在 Hugo v0.95.0(2022 年 3 月 16 日发布)中,应该是

[params]
    date_format = "02 Jan 2006"

---
date: 2022-03-18T21:10:00+07:00
---

在帖子前面,不需要双引号。我使用了Ananke主题


2
投票

在配置文件中设置 date_format 对我来说似乎很奇怪。我会在配置文件中设置以下内容:

defaultContentLanguage: nl
languageCode: nl_NL

这些变量不会进入参数,而是在配置文件的根级别。当您拥有这些时,您只需致电:

{{ .Date | time.Format ":date_long" }} 

或者...如果您需要其他格式:

{{ .Date | time.Format ":date_medium" }}

日期将以您的语言格式化。


1
投票

您需要在 config.toml 文件中设置日期格式为 2.1.2006(任何正确的格式,确保 2, Jan, 2006)。
这个链接拯救了我的日子

© www.soinside.com 2019 - 2024. All rights reserved.