Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/472.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/date/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/wordpress/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在Freemarker模板或javascript中以特定格式转换日期_Javascript_Date_Freemarker - Fatal编程技术网

如何在Freemarker模板或javascript中以特定格式转换日期

如何在Freemarker模板或javascript中以特定格式转换日期,javascript,date,freemarker,Javascript,Date,Freemarker,从json中,我得到的值为 "createdOn": "Jan 08 2015 20:40:56 GMT+0530 (IST)", 我正在访问FTL <#list variables as variable> <div class="reply"> ${variable.createdOn} </div> </#list> 我更喜欢的格式是 09-01-2015 我需要删除GMT、IST等其他时间 如何在Freemarker模板或

从json中,我得到的值为

"createdOn": "Jan 08 2015 20:40:56 GMT+0530 (IST)",
我正在访问FTL

<#list variables as variable>
  <div class="reply">
   ${variable.createdOn}
  </div>
</#list>
我更喜欢的格式是
09-01-2015

我需要删除GMT、IST等其他时间

如何在Freemarker模板或javascript中转换

更新

我试着这样从下面通过

${variable.createdOn?datetime?string("dd-MM-yyyy")}
但它给出了错误的定义

Exception: java.text.ParseException - Unparseable date: "Jan 09 2015 12:36:18 GMT+0530 (IST)"
感谢您的帮助

谢谢你试过这个吗

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

这里是文档链接:

您可以创建自己的自定义函数,并使用
getDate
getMonth
getFullYear
方法格式化日期

请注意,必须将字符串日期格式解析为日期对象


单击按钮以dd-MM-yyyy格式显示当月的今天

试试看

函数myFunction(){ var d=新日期(“2015年1月8日20:40:56 GMT+0530(IST)”;//将字符串日期格式解析为日期对象。 var z=d.getDate()+“-”+(d.getMonth()+1)+“-”+d.getFullYear(); document.getElementById(“demo”).innerHTML=z; }
函数转换日期(日期){
dateSplit=date.toString().split(“”);
dateSplit[1]=date.getMonth()+1<10?'0'+(date.getMonth()+1).toString():date.getMonth()+1;
返回日期拆分[2]+'-'+dateSplit[1]+'-'+dateSplit[3];
}
转换日期(新日期());

这应该可以完成任务。你可以额外调整它

首先,那到底是什么格式?我的意思是,如果你能影响某人改用标准格式(主要是ISO),这将帮助所有人。无论如何,FreeMarker不是一个日期解析器库,但实际上您可以执行以下操作:

<#-- 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}

${createdOn?日期时间(“MMM dd yyyy HH:mm:ss'GMT'Z”)?日期}

我走这条路。我创建了一个对象格式化程序,并将其传递到模板模型中。我在模板中调用formatter.format(date)

template.ftl


已创建${formatter.format(order.Created)}
金额${订单金额!}
货币${order.Currency!}
OrderPresenter.java

@组件
公共类OrderPresenter{
私有静态最终字符串格式化程序_PARAM=“FORMATTER”;
私有静态最终字符串日期\时间\格式=“yyyy-MM-dd HH:MM:ss”;
专用静态最终DateTimeFormatter格式化程序=
模式的DateTimeFormatter.of(日期时间格式).withZone(ZoneId.systemDefault());
私有配置=prepareConfiguration();
公共字符串到HtmlPresentation(ClientDetails ClientDetails){
试一试{
Template Template=configuration.getTemplate(客户机数据模板);
Writer out=新的StringWriter();
流程模板(toMap(clientDetails),out);
return out.toString();
}捕获(IOException | TemplateException e){
抛出新的运行时异常(e);
}
}
私有配置prepareConfiguration(){
配置=新配置(配置。版本2\u 3\u 23);
setDefaultEncoding(编码);
setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW\u HANDLER);
setLogTemplateExceptions(非\u到\u日志\u异常);
setClassForTemplateLoading(OrderPresenter.class,TEMPLATES\u文件夹);
返回配置;
}
私有映射toMap(ClientDetails ClientDetails){
Map res=新的HashMap();
res.put(客户详细信息参数、客户详细信息);
res.put(格式化程序参数,格式化程序);
返回res;
}
}

@Beri,我已经试过了。它给出了
异常:java.text.ParseException-不可解析的日期:“2015年1月9日12:36:18 GMT+0530(IST)”
@rakesh我相信你应该用单引号代替双引号。谢谢你,arman,但这并不能解决我的问题。创建日期可能与昨天、今天或一个月前不同。我的意思是我需要从json中读取它。对不起,我不清楚。我编辑了我的代码。您必须将JSON中的字符串格式解析为日期对象您还想打印选项“昨天”、“上个月”等等-取决于您的日期?@Bowdzone,谢谢您的编辑建议,这是我的第一个回复,我没有注意到:)谢谢。。这是工作的
${createdOn?datetime(“MMM-dd-yyy-HH:mm:ss'GMT'Z”)?date}
function convertDate( date ){
    dateSplit = date.toString().split( ' ' );
    dateSplit[1] = date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1).toString() : date.getMonth() + 1;
    return dateSplit[2] + '-' + dateSplit[1] +  '-' + dateSplit[3];
}

convertDate(new Date());
<#-- 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}
<div class="historyOrderItem">
    <div>
    <div>Created <#if order.created??>${formatter.format(order.created)}</#if></div>
    <div>Amount ${order.amount!}</div>
    <div>Currency ${order.currency!}</div>
</div>
@Component
public class OrderPresenter {

    private static final String FORMATTER_PARAM = "formatter";
    private static final String DATE_TIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
    private static final DateTimeFormatter FORMATTER =
            DateTimeFormatter.ofPattern(DATE_TIME_FORMAT).withZone(ZoneId.systemDefault());

    private Configuration configuration = prepareConfiguration();

    public String toHtmlPresentation(ClientDetails clientDetails) {
        try {
            Template template = configuration.getTemplate(CLIENT_DATA_TEMPLATE);
            Writer out = new StringWriter();
            template.process(toMap(clientDetails), out);
            return out.toString();
        } catch (IOException | TemplateException e) {
            throw new RuntimeException(e);
        }
    }

    private Configuration prepareConfiguration() {
        Configuration configuration = new Configuration(Configuration.VERSION_2_3_23);
        configuration.setDefaultEncoding(ENCODING);
        configuration.setTemplateExceptionHandler(TemplateExceptionHandler.RETHROW_HANDLER);
        configuration.setLogTemplateExceptions(NOT_TO_LOG_EXCEPTIONS);
        configuration.setClassForTemplateLoading(OrderPresenter.class, TEMPLATES_FOLDER);
        return configuration;
    }

    private Map<String, Object> toMap(ClientDetails clientDetails) {
        Map<String, Object> res = new HashMap<>();
        res.put(CLIENT_DETAILS_PARAM, clientDetails);
        res.put(FORMATTER_PARAM, FORMATTER);
        return res;
    }
}