You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Freemarker如何设置日期格式实现月份显示为四位缩写(如Sept)

FreeMarker日期格式化实现九月显示为Sept的方法

你当前使用的MMM格式符调用的是默认语言包的月份缩写规则,英文环境下9月默认输出3位缩写Sep,要实现Sept格式可选择以下两种方案:

方案1:字符串替换(单场景快速适配)

仅需要调整单个页面/单个日期的展示时,直接对格式化后的结果做替换即可,改动最小:

<#-- 先计算目标日期 -->
<#assign targetDate = (.now?long - 259200000)?number_to_date>
<#-- 按原有规则格式化后替换9月的缩写 -->
<#assign formatedDate = targetDate?string['MMM. d']?replace('Sep.', 'Sept.')>
<#-- 输出最终结果 -->
${formatedDate}

方案2:自定义月份缩写(全局统一适配)

如果全项目所有日期都要求9月显示为Sept,可以提前在FreeMarker配置中注入自定义的月份缩写数组,全局复用:

  • 后端FreeMarker配置中注入自定义缩写变量(以Java环境为例):
// 按顺序定义1-12月的缩写,9月对应下标8的位置设置为Sept.
String[] customMonthAbbr = {"Jan.", "Feb.", "Mar.", "Apr.", "May.", "Jun.", "Jul.", "Aug.", "Sept.", "Oct.", "Nov.", "Dec."};
// 设为全局共享变量,所有模板都可调用
configuration.setSharedVariable("customMonthAbbr", customMonthAbbr);
  • 前端模板直接调用自定义变量拼接日期:
<#assign targetDate = (.now?long - 259200000)?number_to_date>
${customMonthAbbr[targetDate?month]} ${targetDate?string['d']}

内容的提问来源于stack exchange,提问作者raieven

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 03:24:04