moment.js template helpers for Ember.js
- Compatibility
- Using Moment.js in Ember Apps & Addons
- Controlling Locale and Timezone Data
- Installing or Upgrading ember-moment
- Usage
- Helpers
- Configuration Options
- Frequently Asked Questions
- Contributing
- Docs to add
- Ember.js v3.16 or above
- Ember CLI v2.13 or above
- Node.js v10 or above
- ember-auto-import 2.0 or above
This addon provides Ember-specific Helpers and a Service that make it convenient to use Moment.js in your templates. If you just want to call Moment.js directly from Javascript, you don't need this addon.
The recommended way to use Moment.js in an Ember app is:
- Add either
moment
ormoment-timezone
to your dependencies. - Make sure you have
ember-auto-import
in your dependencies. - Make sure you don't have
ember-cli-moment-shim
in your dependencies (it would add a redundant copy). - Use imports in your Javascript like
import moment from 'moment'
orimport moment from 'moment-timezone'
. - Optional but strongly recommended: Configure which locales and timezones will be included in your app by following the instructions below in "Controlling Locale and Timezone Data".
The recommended way to use Moment.js in an Ember addon is:
- Add either
moment
ormoment-timezone
to yourpeerDependencies
. This ensures that your addon and the app must use the same copy. - Also add it to your
devDependencies
so your own test suite satisfies the peerDep. - Make sure you don't depend on
ember-cli-moment-shim
. - Make sure you do depend on
ember-auto-import
.
Moment itself is no longer actively developed and new projects should not add it. You can look at alternative libraries like Luxon, or better yet keep an eye on Temporal which is likely to add moment-like capabilities directly to Javascript quite soon.
Apps should configure their locale and timezone support (which can have a large impact on bundle size) by following Moment's documentation about Webpack:
and passing the resulting webpack configuration to either ember-auto-import or Embroider. For example:
const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');
module.exports = function (defaults) {
let app = new EmberApp(defaults, {
autoImport: {
webpack: {
plugins: [
new MomentLocalesPlugin({
// 'en' is built into moment and cannot be removed. This strips the others.
localesToKeep: [],
}),
new MomentTimezoneDataPlugin({
// Keep timezone data for the US, covering all possible years.
matchCountries: 'US',
}),
],
},
},
});
- First, install Moment.js using the instructions above.
- Then you can install ember-moment:
ember install ember-moment
Compatibility:
-
Ember Octane+ : >= v9 // the default branch
-
Ember Classic : < v9 // the ember-classic branch.
the
ember-classic
branch is in maintenance mode, and will only receive bugfixes
Parameters | Values |
---|---|
<date> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
Returns a Moment.
Example
Parameters | Values |
---|---|
<date> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
Returns a Moment with utc mode set.
Example
Parameters | Values |
---|---|
<date> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
outputFormat |
An optional date/time String output format, defaults to moment.defaultFormat which you must explicitly define |
<inputFormat> |
An optional date/time String input format |
Formats a <date>
to an optional outputFormat
from an optional inputFormat
. If the inputFormat
is not provided, the date String
is parsed on a best effort basis. If the outputFormat
is not given the global moment.defaultFormat
is used. Typically, outputFormat
and inputFormat
are given. See momentjs#format
.
NOTE: for all other helpers, the input format string is the second argument.
Example
Parameters | Values |
---|---|
<dateA> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
<dateB> |
An optional value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...), defaults to now |
hideAffix |
An optional Boolean to hide the relative prefix/suffix or not. |
Returns the time between <dateA>
and <dateB>
relative to <dateB>
. See momentjs#from
.
Note that moment-from-now
is just a more verbose moment-from
without dateB
. You don't need to use it anymore.
Examples
Parameters | Values |
---|---|
<dateA> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
<dateB> |
An optional value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...), defaults to now |
hideAffix |
An optional Boolean to hide the relative prefix/suffix or not. |
Returns the time between <dateA>
and <dateB>
relative to <dateA>
. See momentjs#to
.
Note that moment-to-now
is just a more verbose moment-to
without dateB
. You don't need to use it anymore.
Examples
Parameters | Values |
---|---|
<number> |
Any value(s) interpretable as a duration by moment (typically a Number in milliseconds) |
<units> |
An optional String to specify the units of <number> (typically 'seconds' , 'minutes' , 'hours' ...) |
Returns a Duration automatically humanized. See momentjs#duration
.
Examples
Parameters | Values |
---|---|
<dateA> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
<dateB> |
An optional value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) used as a reference, defaults to now |
<formatHash> |
An optional output format hash, defaults to {} |
Returns the time between <dateA>
and <dateB>
relative to <dateB>
in a way that is different from moment-from
and customizable via <formatHash>
. See momentjs#calendar
.
Examples
Parameters | Values |
---|---|
<dateA> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
<dateB> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
precision |
An optional unit of measurement, defaults to 'milliseconds' |
float |
An optional Boolean to get the floating point result, rather than the integer value |
Returns the difference in precision
units between <dateA>
and <dateB>
with floating point accuracy if float
is true
. See momentjs#diff
.
Examples
Parameters | Values |
---|---|
<date> |
An optional value interpretable as a date/time by moment (a date String or a Moment or a Date ...). Defaults to value of moment() |
<number> |
Any value(s) interpretable as a duration by moment that is the amount of the precision you want to add to the date provided |
precision |
An optional unit of measurement, defaults to 'milliseconds' |
Mutates the original moment by adding time. See momentjs#add
.
Examples
Parameters | Values |
---|---|
<date> |
An optional value interpretable as a date/time by moment (a date String or a Moment or a Date ...). Defaults to value of moment() |
<number> |
Any value(s) interpretable as a duration by moment that is the amount of the precision you want to subtract from the date provided |
precision |
An optional unit of measurement, defaults to 'milliseconds' |
Mutates the original moment by removing time. See momentjs#subtract
.
Examples
Parameters | Values |
---|---|
<dateA> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
<dateB> |
An optional value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...). If not given, <dateA> becomes now and <dateB> becomes <dateA> |
precision |
An optional String unit of comparison precision, defaults to 'milliseconds' |
Returns a Boolean
that indicates if <dateA>
is respectively before/after/the same/same or before/ same or after <dateB>
to the precision
level. See momentjs#queries
.
Examples
Parameters | Values |
---|---|
<date> |
Any value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
<dateA> |
A boundary value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...) |
<dateB> |
An optional boundary value(s) interpretable as a date/time by moment (a date String or a Moment or a Date ...). If not given <date> is assigned now, <dateA> is assigned <date> and <dateB> is assigned <dateA> . |
precision |
An optional String unit of comparison precision, defaults to 'milliseconds' |
inclusivity |
An optional String indicating inclusivity of the boundaries, defaults to () |
Returns a Boolean
that indicates if <date>
is between <dateA>
and <dateB>
to the precision
level and with boundary inclusions specified by inclusivity
. See momentjs#is-between
.
Examples
Returns the present Moment.
Examples
Parameters | Values |
---|---|
<timestamp> |
An integer Number or String value representing the number of seconds since the Unix Epoch (January 1 1970 12AM UTC) |
Returns a Moment corresponding to the <timestamp>
.
Examples
All helpers accept the following optional named arguments (even though they are not always applicable):
Parameters | Values |
---|---|
locale |
An optional String locale, to override the default global moment.locale |
timeZone |
An optional String time zone, defaults to moment.timeZone (the default time zone) |
interval |
An optional interval Number of milliseconds when the helper should be recomputed |
allow-empty |
An optional Boolean to ignore the Invalid date output when knowingly passing null , undefined , or '' , defaults to false |
Note that interval
does not recompute the value of the helper parameters, unless it is
part of a helper that is a value in which case it is useful for "live" updating as time elapses.
Warning: allow-empty
is currently inconsistent and should not always be trusted.
Examples
Your application may require a default format other than the default, ISO 8601. For example, you may want dates to fallback on the localized shorthand format L
by default.
// config/environment.js
module.exports = function() {
return {
moment: {
outputFormat: 'L'
}
}
};
If you need to change the default format during runtime, use the service API. Doing so will cause the moment-format helper instances to re-render with the new default format.
// app/controller/index.js
export default Ember.Controller.extend({
moment: Ember.inject.service(),
actions: {
changeDefaultFormat() {
this.set('moment.defaultFormat', 'MM.DD.YYYY');
}
}
})
If null
, undefined
, or an empty string are passed as a date to any of the moment helpers then you will get Invalid Date
in the output. To avoid this issue globally, you can set the option allowEmpty
which all of the helpers respect and will result in nothing being rendered instead of Invalid Date
.
// config/environment.js
module.exports = function() {
return {
moment: {
allowEmpty: true // default: false
}
}
};
// app/routes/applicaton.js
export default Ember.Route.extend({
moment: Ember.inject.service(),
beforeModel() {
this.get('moment').setLocale('es');
}
});
// app/routes/applicaton.js
export default Ember.Route.extend({
moment: Ember.inject.service(),
beforeModel() {
this.get('moment').setTimeZone('America/Los_Angeles');
}
});
Invalid Date
is being rendered into the DOM, how do I avoid this?
An invalid date string is being passed into momentjs and/or the input string format was omitted.
If you are knowingly passing null, undefined, or an empty string and want to ignore the output of Invalid Date
then pass the option allow-empty=true
to the helper (all helpers accept this property)
See the Contributing guide for details.