Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to format date in meteor template

I need to display a date from database in the format 'mm-dd-yyyy'. As its saved in ISO format in mongodb how can I convert it in the template ? Here is my code.

       Template.templatename.vname = function () {               return Posts.find();          } 

And in template

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

Now its getting displayed like Tue Feb 04 2014 00:00:00 GMT+0530 (IST)

I need to show it as mm-dd-yyyy

like image 649
I'm nidhin Avatar asked Feb 28 '14 06:02

I'm nidhin


People also ask

How do you format a date?

Press CTRL+1. In the Format Cells box, click the Number tab. In the Category list, click Date, and then choose a date format you want in Type.

How do you format mm dd yyyy?

yyyy-MM-dd — Example: 2013-06-23.

How do I change the date format in Visual Studio?

In "Control Panel\All Control Panel Items\Region and Language", on the "Formats" tab, click "Additional settings..." button. Visual Studio 2008 debugger formats dates using "Short date:" from the "Date" tab of "Customize Format".


1 Answers

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.

like image 146
David Weldon Avatar answered Sep 21 '22 20:09

David Weldon