I’m trying to create a project in Meteor, which should work just like Facebook etc. At standard, when using Collection2, i’m inserting the createdAt date into my mongoDB. When printet out on the site, the date looks like this:
Wed Mar 16 2016 12:07:08 GMT+0100 (CET)
I’ve looked at MomentJS but can’s seem to use it in practice. I want to date to be a human readable date, where it says “2 hours ago”, “17. february 19:25” and so on.
Can anyone help me with that and maybe show give me an example of how to use it?
you should definitively use momentJS. It is a very good library
First you have to convert this date to a duration.
Then you can humanize it and get the result you want
Hey @brajt I’ve trying working with it, but can’t seem to make it work. In my application i have these posts. I have the following:
Posts.html
Posts.js
Post.html
Post.js
I’m guessing that i should use the helper in Posts.js, where i print out all the posts. But when i try to return the date, nothing happens. I’ve tried {{fromNow}} and console.log but nothing shows.
Can you show us your code? What your template helper looks like, where/how your attempting to show the date in your HTML, etc. Any code you can help share will help us troubleshoot.
Somewhere in your client code (probably in posts.js) you define a template helper to do the formatting, e.g.
Template.Posts.helpers({
myCustomTemplateHelper: function (dateTime) {
return moment(dateTime).fromNow();
}
});
When rendering the template Meteor will encounter a reference to your template helper {{myCustomTemplateHelper ...
This will call the helper function - you pass the createdAt date/time into the helper by putting it after the template helper name, so it reads like this: {{myCustomTemplateHelper createdAt}}
createdAt will become the dateTime argument in the helper
The custom template handler will be executed, formatting the date for you in whatever way you want it to do so (see momentjs docs) and return it
The result of the helper will be added in place of the {{myCustomTemplateHelper createdAt}}
is there any way to make this reactive? Like when the post is created it says “a few seconds ago”. When it has been up for like a minute or so, then automatically update. Just like this date on the site.