The format of numbers and dates in dust.js (connected by a fork)

How can I format numbers, currency or date values ​​in the dust.js template?

Data:

{ today: 'Wed Apr 03 2013 10:23:34 GMT+0200 (CEST)' } 

Template:

 <p>Today: {today} </p> 

Similarly: (with moment.js)

 <p>Today: {moment(today).format('dd.MM.YYYY')}</p> 

Or round some price values ​​*

Data: {Price: 56.23423425}

Template:

Price: {price.toFixed (2)}

+6
source share
3 answers

You will probably need to write an assistant. Details on how to write an assistant can be found here:

Your template for the Date string will look like this:

 <p>Today: {@formatDate value="{today}"/}</p> 

Your assistant will be something like this:

 dust.helpers.formatDate = function (chunk, context, bodies, params) { var value = dust.helpers.tap(params.value, chunk, context), timestamp, month, date, year; timestamp = new Date(value); month = timestamp.getMonth() + 1; date = timestamp.getDate(); year = timestamp.getFullYear(); return chunk.write(date + '.' + month + '.' + year); }; 

You would like to add a part to get the leading zero before the month or date.

+8
source

For people who need this for a nodeJs application, here is a good example of KrakenJS:

https://github.com/lmarkus/Kraken_Example_Date_Format_Helper

It uses Moment.js to avoid reusing the wheel when formatting the date.

+3
source

You can write a filter to use the moment. Put it this way: dust.filters.formatDate = (value) => moment.utc (value) .format ('l H: mm'); where is correct in your script. then in your html just put | Beyond Your Value: {Date | FormatDate}

+1
source

All Articles