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");
});
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, 2022Comments
-
Nyxynyx almost 2 years
When using Meteor's Handlebar bracers, how do you convert the output of
{{ timestamp }}
fromThu Jul 25 2013 19:33:19 GMT-0400 (Eastern Daylight Time)
toJul 25
?Tried
{{ timestamp.toString('yyyy-MM-dd') }}
but it gave an error-
Jonathan Lonowski almost 11 yearsNote: The standard
toString()
forDate
s ignores any arguments passed to it and ECMAScript doesn't define any other methods that can format aDate
based on aString
like'yyyy-MM-dd'
. If you're not already including a library which modifiestoString()
, have a look at stackoverflow.com/q/1056728 for suggestions. -
Boris Kotov over 10 yearshere's a better description how to do this -> stackoverflow.com/questions/18580495/…
-
-
Spencer over 10 yearsAs @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 about 9 years@Akshat, note that after some flip flopping, the syntax is now
Template.registerHelper