{{moment}} ![NPM version](https://camo.githubusercontent.com/f579092e8d9229412f1b005252f7e1efc8ed6100564e882398868f9eb1802657/68747470733a2f2f62616467652e667572792e696f2f6a732f68616e646c65626172732d68656c7065722d6d6f6d656e742e706e67)
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 path
helpers: ['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}}
meansmoment().method()
, and\{{moment somedate method="something"}}
meansmoment(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:
Results in:
1372599296756
Current time with formatting
Results in:
16:34:56
Specific time with formatting
Results in:
01:53:23
Specific unix timestamp with formatting
Results in:
19:17:56
Specific datetime with formatting
Results in:
Sunday, 30 June 2013 01:53:23 +0300
Language support
Results in:
sunnuntai, 30 kesäkuu 2013 01:53:23 +0300
Is the added date valid?
Results in:
true
Input as a string, with defined format
Results in:
June 2013
Multiple formats
When multiple formats are used, moment.js attempts to use the "correct" one
Results in:
June 2013
Specific datetime with formatting, converted to UTC
Results in:
Saturday, 29 June 2013 22:53:23 +0000
Getters and setters
Example: get week of year, depends on lang (see moment.js documentation for more details)
Results in:
The week number in Finland is 26
Manipulating
See the moment.js/manipulating documentation for more details.
add days
Results in:
Sunday-Friday
subtract years
Results in:
2008-2013
startOf month
Results in:
Saturday, 01 June 2013 00:00:00 +0300
endOf week
Results in:
Saturday, 06 July 2013 23:59:59 +0300
Time from now
Results in:
After this page is rendered, the week will end in 6 days
Time from X
Results in:
This helper was coded in 15 hours
Calendar time
Results in:
The coding started Today at 1:53 AM
Diff
Results in:
The difference between those two moments is 52893769
Days in Month
Results in:
This month has 30 days
Days in Month
Results in:
This month has 30 days
Duration, with humanization
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.
Author
Mikko Tapionlinna
License
Copyright (c) 2014 Mikko Tapionlinna, contributors. Released under the MIT license
This file was generated on Sunday, May 4, 2014.