在Meteor中的Handlebars模板中格式化日期 [英] Format a date from inside a Handlebars Template in Meteor

查看:349
本文介绍了在Meteor中的Handlebars模板中格式化日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



如下所示:

我从数据中获得了ISO格式的日期,我实际想要做的是直接从我的模板修改日期格式。 / p>

  {{format my.context.datemyFormat}} 

我正在使用时刻库​​,所以我可以这样写:

 code> {{formatDate my.context.dateDD.MM.YYYY HH:mm}} // 03.09.2013 18:12 

这样会很好,因为我认为这是我能够做到这一点的地方。在我的模板中。

解决方案

解决方案很简单,也许有人会发现它很有用。
在大多数项目中,您都需要使用几种日期格式。
所以这是一个很好的方法来定义你的格式与可读的名称。



对于这个例子,我采取了短和长,但你会看到,这很容易扩展。



所以我在我的客户端脚本中创建了一个对象:

  var DateFormats = {
short:DD MMMM - YYYY,
long:dddd DD.MM.YYYY HH:mm
};

此外,我创建了一个Handlebars帮助器formatDate。



已编辑:现在您应该使用UI而不是Handlebars

  //自版本0.8.0以来已弃用
Handlebars.registerHelper(formatDate,function(datetime,format){

//使用UI.registerHelper ..
UI.registerHelper(formatDate,function(datetime,format) {
if(moment){
//可以使用其他格式,如'lll'太
格式= DateFormats [格式] ||格式;
返回时刻(datetime).format (格式);
}
else {
return datetime;
}
});

正如你所看到的,我在我的助手中使用了moment.js lib,要安装它,只需输入 meteor add momentjs:moment 从您的命令行。



现在,我的模板中的任何地方我可以使用它与两个参数,如下所示:

  {{formatDate MyISOStringshort}} // 02 September  -  2013 
{ {formatDate MyISOStringlong}} //星期一02.09.2013 18:00

如果你想创建自己的格式,看一下timjs文档 http://momentjs.com/docs/



快乐编码!


I got a ISO formatted Date from my Data and what I actually want to do, is to modify my date format directly from my Templates.

like this:

{{format my.context.date "myFormat"}}

I'm using the moment library, so I could write something like this:

{{formatDate my.context.date "DD.MM.YYYY HH:mm"}} // 03.09.2013 18:12

It would be nice, because I think it's the place where I should be able to do this. In my template.

解决方案

The solution is quite simple, and maybe someone will find it useful. In most projects you have a couple of date formats you want to use. So it's a good approach to define your formats with readable names.

For this example I took just 'short' and 'long', but you will see, it's very easy to extend.

So I created an Object in my Client Script:

var DateFormats = {
       short: "DD MMMM - YYYY",
       long: "dddd DD.MM.YYYY HH:mm"
};

Also, I created a Handlebars Helper "formatDate".

Edited: Now you should use UI instead of Handlebars

// Deprecated since version 0.8.0 
Handlebars.registerHelper("formatDate", function(datetime, format) {

// Use UI.registerHelper..
UI.registerHelper("formatDate", function(datetime, format) {
  if (moment) {
    // can use other formats like 'lll' too
    format = DateFormats[format] || format;
    return moment(datetime).format(format);
  }
  else {
    return datetime;
  }
});

As you can see, I use the moment.js lib in my Helper. To install it, just type meteor add momentjs:moment from your command line.

And now, everywhere in my Templates I can use it with the two params, like this:

{{formatDate MyISOString "short"}} // 02 September - 2013
{{formatDate MyISOString "long"}} //  Monday 02.09.2013 18:00

If you want to create your own formats, take a look at the momentjs docs http://momentjs.com/docs/

Happy coding!

这篇关于在Meteor中的Handlebars模板中格式化日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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