feat(user/questions): 手机端时间跨年自动补年份,避免省略年份后产生歧义
DateCompact:与当前同一年的渲染「月-日 时:分」,跨年补回完整「年-月-日 时:分」。 只砍年份会让「去年今天」和「今年今天」显示成同一串,逐条补年份比按年分组便宜得多 (列表带游标分页 + 可切排序/筛选,组标题会跨页重复或错位)。 桌面端不受影响,仍显示 Y-m-d H:i:s。 测试覆盖年份边界(同年 1/1 与 12/31、跨年);预览数据里放了一条 2025 年的提问用于肉眼核对。
This commit is contained in:
@@ -47,6 +47,9 @@ func FuncMap() []template.FuncMap {
|
||||
format = replacer.Replace(format)
|
||||
return t.Format(format)
|
||||
},
|
||||
"DateCompact": func(t time.Time) string {
|
||||
return dateCompact(t, time.Now())
|
||||
},
|
||||
"QuestionFormat": func(input string) template.HTML {
|
||||
input = html.EscapeString(input)
|
||||
input = strings.ReplaceAll(input, "\n", "</br>")
|
||||
@@ -236,3 +239,15 @@ func FuncMap() []template.FuncMap {
|
||||
func Safe(raw string) template.HTML {
|
||||
return template.HTML(raw)
|
||||
}
|
||||
|
||||
// dateCompact 手机端列表用的紧凑时间:与 now 同一年的只给「月-日 时:分」。
|
||||
// 手机一行的宽度放不下「Y-m-d H:i:s」(单它就要 127px),但只砍年份会让
|
||||
// 「去年的今天」和「今年今天」渲染成同一串,所以在跨年时自动补回年份。
|
||||
// 注意:这是「逐条补年份」,不是「按年分组」—— 列表带游标分页、可切排序与筛选,
|
||||
// 分组标题会跨页重复/错位,收益不如补年份直接。
|
||||
func dateCompact(t, now time.Time) string {
|
||||
if t.Year() == now.Year() {
|
||||
return t.Format("01-02 15:04")
|
||||
}
|
||||
return t.Format("2006-01-02 15:04")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user