如何在Freemarker模板或javascript中以特定格式转换日期 [英] How to convert date in specific format in Freemarker template or javascript

查看:336
本文介绍了如何在Freemarker模板或javascript中以特定格式转换日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

来自json,我的价值为

From json, i am getting the value as

"createdOn": "Jan 08 2015 20:40:56 GMT+0530 (IST)",

我在FTL中访问

<#list variables as variable>
  <div class="reply">
   ${variable.createdOn}
  </div>
</#list>

我得到的结果是

Jan 09 2015 12:36:18 GMT+0530 (IST)

我的格式是
09-01-2015

My preferable format is 09-01-2015

我需要删除剩余的时间GMT,IST等。

I need to remove rest of the time GMT, IST and so on.

如何在Freemarker模板或javascript中转换它。

How to convert this in Freemarker template or javascript.

更新

我试图像这样传递下面

${variable.createdOn?datetime?string("dd-MM-yyyy")}

但它给出了错误

Exception: java.text.ParseException - Unparseable date: "Jan 09 2015 12:36:18 GMT+0530 (IST)"

任何帮助都是赞赏的。

谢谢

推荐答案

首先,它的格式是什么?我的意思是,如果你可以影响某人使用标准格式(主要是ISO),这将有助于每个人。无论如何,FreeMarker不是一个日期解析器库,但实际上你可以这样做:

First of all, what format is that at all? I mean, if you can influence someone to use a standard format instead (ISO, mostly) that will help everyone. Anyway, FreeMarker isn't a date parser library, but actually you can do something like this:

<#-- Settings you need -->
<#setting date_format="dd-MM-yyyy">
<#setting locale="en_US">

<#-- The string that comes from somewhere: -->
<#assign createdOn = 'Jan 08 2015 20:40:56 GMT+0530 (IST)'>

<#--
  1. Tell FreeMarker to convert string to real date-time value
  2. Convert date-time value to date-only value
  3. Let FreeMarker format it according the date_format setting
-->
${createdOn?datetime("MMM dd yyyy HH:mm:ss 'GMT'Z")?date}

这篇关于如何在Freemarker模板或javascript中以特定格式转换日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆