Formats a date into a string given a format. Accepts any value that can be passed to new Date()
. This helper is a port of the formatDate-js library by Michael Baldry.
Parameters: format String
, required
The format string, according to these tokens: strftime
Given this data:
date = new Date()
And these templates:
{{formatDate date "%m/%d/%Y"}}
{{formatDate date "%I:%M%p"}}
{{formatDate date "%F"}}
{{formatDate date "%Y%m%dT%H%M%S%z"}}
The output would be:
07/26/2012
11:38PM
2012-07-26
20120726T233805-0004
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.
Install the helper:
npm i helper-moment --save-dev
Now add the helper to Assemble's options:
assemble: {
options: {
// Assemble will automatically resolve the path
helpers: ['helper-moment', 'foo/*.js' ]
}
}
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.
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.
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: 5
timeago:
years: 5
duration:
hours: 2
minutes: 33}
---
Unix timestamp of now
{{moment }}
Results in:
1372599296756
{{moment format ="HH:mm:ss" }}
Results in:
16:34:56
{{moment example format ="HH:mm:ss" }}
Results in:
01:53:23
{{moment unixtimestamp format ="HH:mm:ss" }}
Results in:
19:17:56
{{moment example format ="dddd, DD MMMM YYYY HH:mm:ss ZZ" }}
Results in:
Sunday, 30 June 2013 01:53:23 +0300
{{moment example lang ="fi" format ="dddd, DD MMMM YYYY HH:mm:ss ZZ" }}
Results in:
sunnuntai, 30 kesäkuu 2013 01:53:23 +0300
Is the added date valid?
{{moment example isValid =null }}
Results in:
true
{{moment [exampletextdate,"ddd, DD MMM YYYY HH:mm:ss ZZ" ] format ="MMMM YYYY" }}
Results in:
June 2013
When multiple formats are used, moment.js attempts to use the "correct" one
{{moment [exampletextdate,formats ] format ="MMMM YYYY" }}
Results in:
June 2013
{{moment example UTC =null format ="dddd, DD MMMM YYYY HH:mm:ss ZZ" }}
Results in:
Saturday, 29 June 2013 22:53:23 +0000
Example: get week of year, depends on lang (see moment.js documentation for more details)
The week number in Finland is {{moment example lang ="fi" week =null }}
Results in:
The week number in Finland is 26
See the moment.js/manipulating documentation for more details.
{{moment example format ="dddd" }} -{{moment example add =daysadd format ="dddd" }}
Results in:
Sunday-Friday
{{moment example subtract =timeago format ="YYYY" }} -{{moment example format ="YYYY" }}
Results in:
2008-2013
{{moment example startOf ="month" format ="dddd, DD MMMM YYYY HH:mm:ss ZZ" }}
Results in:
Saturday, 01 June 2013 00:00:00 +0300
{{moment example endOf ="week" format ="dddd, DD MMMM YYYY HH:mm:ss ZZ" }}
Results in:
Saturday, 06 July 2013 23:59:59 +0300
After this page is rendered, the week will end {{moment endOf ="week" fromNow =null }}
Results in:
After this page is rendered, the week will end in 6 days
This helper was coded {{moment from =example }}
Results in:
This helper was coded in 15 hours
The coding started {{moment example calendar =null }}
Results in:
The coding started Today at 1:53 AM
The difference between those two moments is {{moment diff =example }}
Results in:
The difference between those two moments is 52893769
This month has {{moment daysInMonth =null }} days
Results in:
This month has 30 days
This month has {{moment daysInMonth =null }} days
Results in:
This month has 30 days
The event will last {{duration duration humanize =null }}
Results in:
The event will last 3 hours
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.
Mikko Tapionlinna
Copyright (c) 2013 Mikko Tapionlinna, contributors. Released under the MIT license
This file was generated on Sunday, October 13, 2013.
Returns the current date.
Parameters: format String
- The format string, according to these tokens:
http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime
(Optional)
Template:
{{now}}
{{now "%m/%d/%Y"}}
Renders to:
Thu Jul 26 2012 23:41:02 GMT-0400 (AST)
07/26/2012
Returns a human-readable time phrase from the given date.
Parameters: none
Data:
date = 'Thu Jul 22 2012 23:41:02 GMT-0400 (AST)'
Template:
{{timeago date}}
Renders to:
4 days ago
See the template for this page →
Find an error? Let us know →