How to format date in meteor template

走远了吗. 提交于 2019-11-27 11:47:53
David Weldon

You may want to create a global helper like:

Template.registerHelper('formatDate', function(date) {
  return moment(date).format('MM-DD-YYYY');
});

Then you can use it like:

{{#each vname}}
  {{formatDate date}}
{{/each}}

This solution depends on moment which is a handy date manipulation library. If you prefer to produce the string without using moment, there are a number of answers for this including this one.

moment is a greet lib

meteor add momentjs:moment

use moment in helper

Template.home.helpers({
    momentFormate: function(time) {

        if ((moment().unix() - moment(time).unix()) < 3600) {
            return moment(time).fromNow();
        } else {
            return moment(time).format("YYYY-MM-DD HH:mm");
        }
    },

    })

Here is a solution that works in Meteor without any dependency on another package:

// global helper
Template.registerHelper('formatDate', function(date) {
    return monthNames[date.getMonth()] + " " + date.getDate() + ", " + date.getFullYear();
});

This will return a date string formatted as "December 11, 2015". Move around getMonth(), getDate() and getFullYear() for your preferred format. For more formatting options, check out other methods of Date object.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!