简体   繁体   English

在速度模板中格式化日期的问题

[英]problem with formatting date inside velocity template

im trying to create a sitemap via velocity template in dotcms我试图通过 dotcms 中的速度模板创建站点地图

this is my code这是我的代码

#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>
        <lastmod>$date.format('medium', $article.modDate) $article.modDate</lastmod>
    </url>
    #end
</urlset>

i am able to render the $article.modDate, but if i am to format it using dateViewtool it doesnt seem to format it and just display the string "$date.format('medium', $article.modDate)" when viewed in the browser.我能够呈现 $article.modDate,但是如果我要使用 dateViewtool 对其进行格式化,它似乎不会对其进行格式化,并且在查看时仅显示字符串“$date.format('medium', $article.modDate)”浏览器。

see screenshot below.看下面的截图。

在此处输入图片说明

the dotcms content type api is throwing the dates as string. dotcms 内容类型 api 将日期作为字符串抛出。 So i have to convert them first into dates and then i can do the formatting所以我必须先将它们转换为日期,然后我才能进行格式化

<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
    #foreach($article in $articles.contentlets)
    #set($md = $date.toDate("yyyy-MM-dd HH:mm:ss",$article.modDate))
    <url>
        <loc>$!{host}$!{article.path}</loc>
        <changefreq>always</changefreq>
        <priority>0.6</priority>
        <lastmod>$date.format("dd MMM yyyy HH:mm:ss", $md)</lastmod>
    </url>
    #end
</urlset>

this works for me这对我有用

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM