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

dotCMS中Velocity模板制作站点地图时日期格式化异常问题

问题原因

  • 通过$json.fetch拉取到的modDate为字符串类型,并非Date对象,$date.format方法仅支持接收Date类型参数,参数类型不匹配时Velocity会直接输出原表达式字符串。
  • 小概率原因为$date(DateViewTool)未在当前模板上下文被正确注册,可先按第一个原因排查。

解决方案

先将字符串类型的modDate转换为Date对象后再调用格式化方法,修改后的代码如下:

#set($articles = $json.fetch('https://xxxxx.xxxx.xxx/api/content/render/false/type/json/query/+contentType:YmArticlePage%20+(conhost:b5d82078-a844-4104-9664-4348c2420bba%20conhost:SYSTEM_HOST)%20+(wfscheme:d61a59e1-a49c-46f2-a929-db2b4bfa88b2*)%20+(wfstep:dc3c9cd0-8467-404b-bf95-cb7df3fbc293*)%20/limit/1/orderby/score,modDate%20desc'))
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    #foreach($article in $articles.contentlets)
    <url>
        <loc>$!{host}$!{article.path}</loc>
        <changefreq>always</changefreq>
        <priority>0.6</priority>
        ## 先转换为Date对象,默认转换失败可手动指定返回的日期字符串匹配格式,例:$date.toDate("yyyy-MM-dd HH:mm:ss", $article.modDate)
        #set($modDateObj = $date.toDate($article.modDate))
        <lastmod>$date.format('medium', $modDateObj)</lastmod>
    </url>
    #end
</urlset>

如果默认toDate转换失败,可根据接口返回的modDate实际字符串格式,手动传入匹配的格式串即可。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.28 13:36:09