Format date in Meteor Handlebars bracers {{ timestamp }}

17,036

Solution 1

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();
});

Solution 2

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;
});

Solution 3

This worked for me

Handlebars.registerHelper("prettifyDate", function(timestamp) {
     return (new Date(timestamp)).format("yyyy-MM-dd");
});
Share:
17,036
Nyxynyx
Author by

Nyxynyx

Hello :) I have no formal education in programming :( And I need your help! :D These days its web development: Node.js Meteor.js Python PHP Laravel Javascript / jQuery d3.js MySQL PostgreSQL MongoDB PostGIS

Updated on June 18, 2022

Comments

  • Nyxynyx
    Nyxynyx almost 2 years

    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

    • Jonathan Lonowski
      Jonathan Lonowski almost 11 years
      Note: The standard toString() for Dates ignores any arguments passed to it and ECMAScript doesn't define any other methods that can format a Date based on a String like 'yyyy-MM-dd'. If you're not already including a library which modifies toString(), have a look at stackoverflow.com/q/1056728 for suggestions.
    • Boris Kotov
      Boris Kotov over 10 years
      here's a better description how to do this -> stackoverflow.com/questions/18580495/…
  • Spencer
    Spencer over 10 years
    As @JonathanLonowski noted above, it's worth noting that timestamp.toString() will ignore any arguments passed to it. I find that using MomentJS makes all of this incredibly simple.
  • KyleMit
    KyleMit about 9 years
    @Akshat, note that after some flip flopping, the syntax is now Template.registerHelper