Add a template function about date 补充一个日期相关的模板函数 (#9815)

* Update time.go

添加一个与日期相关的模板函数,有「星期天」了

* Update template.go

添加一个日期相关的模板函数
This commit is contained in:
Jeffrey Chen 2023-12-06 08:56:00 +08:00 committed by GitHub
parent 7acc6bd02d
commit 3dc72e25da
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 10 additions and 0 deletions

View file

@ -49,6 +49,7 @@ func RenderGoTemplate(templateContent string) (ret string, err error) {
tmpl = tmpl.Funcs(template.FuncMap{ tmpl = tmpl.Funcs(template.FuncMap{
"Weekday": util.Weekday, "Weekday": util.Weekday,
"WeekdayCN": util.WeekdayCN, "WeekdayCN": util.WeekdayCN,
"WeekdayCN2": util.WeekdayCN2,
"ISOWeek": util.ISOWeek, "ISOWeek": util.ISOWeek,
}) })
tpl, err := tmpl.Parse(templateContent) tpl, err := tmpl.Parse(templateContent)
@ -249,6 +250,7 @@ func renderTemplate(p, id string, preview bool) (string, error) {
} }
funcMap["Weekday"] = util.Weekday funcMap["Weekday"] = util.Weekday
funcMap["WeekdayCN"] = util.WeekdayCN funcMap["WeekdayCN"] = util.WeekdayCN
funcMap["WeekdayCN2"] = util.WeekdayCN2
funcMap["ISOWeek"] = util.ISOWeek funcMap["ISOWeek"] = util.ISOWeek
goTpl := template.New("").Delims(".action{", "}") goTpl := template.New("").Delims(".action{", "}")

View file

@ -40,6 +40,14 @@ func WeekdayCN(date time.Time) string {
return weekdayCN[week] return weekdayCN[week]
} }
// WeekdayCN2 returns the day of the week specified by date.
// Sunday=天, Monday=一, ..., Saturday=六.
func WeekdayCN2(date time.Time) string {
week := Weekday(date)
weekdayCN2 := []string{"天", "一", "二", "三", "四", "五", "六"}
return weekdayCN2[week]
}
// ISOWeek returns the ISO 8601 year and week number in which date occurs. // ISOWeek returns the ISO 8601 year and week number in which date occurs.
// Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1, // Week ranges from 1 to 53. Jan 01 to Jan 03 of year n might belong to week 52 or 53 of year n-1,
// and Dec 29 to Dec 31 might belong to week 1 of year n+1. // and Dec 29 to Dec 31 might belong to week 1 of year n+1.