Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Format date in Meteor Handlebars bracers {{ timestamp }}

When using Meteor's Handlebar bracers, how do you convert the output of {{ timestamp }} from Thu Jul 25 2013 19:33:19 GMT-0400 (Eastern Daylight Time) to Jul 25?

Tried {{ timestamp.toString('yyyy-MM-dd') }} but it gave an error

like image 305
Nyxynyx Avatar asked Jul 26 '13 06:07

Nyxynyx


2 Answers

Use a handlebars helper:

Template.registerHelper("prettifyDate", function(timestamp) {
    return new Date(timestamp).toString('yyyy-MM-dd')
});

Then in your html:

{{prettifyDate timestamp}}

If you use moment:

Template.registerHelper("prettifyDate", function(timestamp) {
    return moment(new Date(timestamp)).fromNow();
});
like image 66
Tarang Avatar answered Sep 22 '22 08:09

Tarang


This works for me.

toString("yyyy-MM-dd") - doesn't convert it.

Template.registerHelper("prettifyDate", function(timestamp) {
    var curr_date = timestamp.getDate();
    var curr_month = timestamp.getMonth();
    curr_month++;
    var curr_year = timestamp.getFullYear();
    result = curr_date + ". " + curr_month + ". " + curr_year;
    return result;
});
like image 34
Matej Avatar answered Sep 18 '22 08:09

Matej