You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
A helper to master time! Combining the powers of Assemble, Handlebars.js and Moment.js. This helper leverages Moment.js to provide ultimate control over manipulating time and dates in your templates.
Quickstart
Install the helper:
npm i handlebars-helper-moment --save-dev
Now add the helper to Assemble's options:
assemble: {options: {// Assemble will automatically resolve the pathhelpers: ['handlebars-helper-moment','foo/*.js']}}
Options
The moment.js lib has plenty of features and options, these examples are just the tip of the iceberg of what moment.js can do.
Tips
Remember that:
\{{moment method=null}} means moment().method(), and
\{{moment somedate method="something"}} means moment(somedate).method("something").
Also, the handlebars syntax does not allow you pass certain values (like arrays and objects) directly from the tag, so you may need to use YAML frontmatter or supply JSON/YAML data to run those.
Usage Examples
Optional YAML Front Matter
YAML front matter is not required, but we'll use it hear to supply our example data for purposes of demonstration:
---example: <%= new Date("Sun Jun 30 2011 01:53:23 GMT+0300 (EEST)") %>exampletextdate: 'Sun, 30 Jun 2012 11:19:15 +0300'inputformats: ["ddd, DD MMM YYYY HH:mm:ss ZZ", "MMMM YYYY"]daysadd:days: 5timeago:years: 5duration:hours: 2minutes: 33}---
Unix timestamp of now
\{{moment}}
After this page is rendered, the week will end \{{momentendOf="week"fromNow=null}}
Results in:
After this page is rendered, the week will end in 6 days
Time from X
This helper was coded \{{momentfrom=example}}
Results in:
This helper was coded in 15 hours
Calendar time
The coding started \{{momentexamplecalendar=null}}
Results in:
The coding started Today at 1:53 AM
Diff
The difference between those two moments is \{{momentdiff=example}}
Results in:
The difference between those two moments is 52893769
Days in Month
This month has \{{momentdaysInMonth=null}} days
Results in:
This month has 30 days
Days in Month
This month has \{{momentdaysInMonth=null}} days
Results in:
This month has 30 days
Duration, with humanization
The event will last \{{durationdurationhumanize=null}}
Results in:
The event will last 3 hours
Contributing
In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint and test your code using grunt.