Gathering detailed insights and metrics for ember-moment
Gathering detailed insights and metrics for ember-moment
Gathering detailed insights and metrics for ember-moment
Gathering detailed insights and metrics for ember-moment
ember-cli-moment-shim
Brings moment and moment-timezone into your Ember applications
ember-power-calendar-moment
The default blueprint for ember-cli addons.
ember-require-module
Easily require a module which will silently fail if it is not found.
ember-pikaday
A tested datepicker component for Ember CLI projects including test helpers for your own acceptance tests.
npm install ember-moment
68.9
Supply Chain
91.8
Quality
80.8
Maintenance
100
Vulnerability
97.9
License
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
399 Stars
634 Commits
122 Forks
18 Watching
7 Branches
70 Contributors
Updated on 02 Sept 2024
JavaScript (88.94%)
Handlebars (8.22%)
HTML (2.47%)
CSS (0.37%)
Cumulative downloads
Total Downloads
Last day
-1.7%
5,862
Compared to previous day
Last week
0.2%
28,441
Compared to previous week
Last month
8.2%
126,604
Compared to previous month
Last year
-12.3%
1,898,001
Compared to previous year
1
2
18
moment.js template helpers for Ember.js
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:
moment
or moment-timezone
to your dependencies.ember-auto-import
in your dependencies.ember-cli-moment-shim
in your dependencies (it would add a redundant copy).import moment from 'moment'
or import moment from 'moment-timezone'
.The recommended way to use Moment.js in an Ember addon is:
moment
or moment-timezone
to your peerDependencies
. This ensures that your addon and the app must use the same copy.devDependencies
so your own test suite satisfies the peerDep.ember-cli-moment-shim
.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:
1const MomentLocalesPlugin = require('moment-locales-webpack-plugin');
2const MomentTimezoneDataPlugin = require('moment-timezone-data-webpack-plugin');
3
4module.exports = function (defaults) {
5 let app = new EmberApp(defaults, {
6 autoImport: {
7 webpack: {
8 plugins: [
9 new MomentLocalesPlugin({
10 // 'en' is built into moment and cannot be removed. This strips the others.
11 localesToKeep: [],
12 }),
13
14 new MomentTimezoneDataPlugin({
15 // Keep timezone data for the US, covering all possible years.
16 matchCountries: 'US',
17 }),
18 ],
19 },
20 },
21 });
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
1{{moment <date>}}
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
1{{moment '12-25-1995' 'MM-DD-YYYY'}} {{!-- Mon Dec 25 1995 00:00:00 GMT-0500 --}}
1{{utc <date>}} 2{{utc}}
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
1{{utc '2001-10-31T08:24:56'}} {{!-- Wed Oct 31 2001 08:24:56 GMT+0000 --}} 2{{utc}} {{!-- current time utc, like Mon Feb 12 2018 20:33:08 GMT+0000 --}} 3{{utc (moment '2018-01-01T00:00:00+01:00' 'YYYY-MM-DD HH:mm:ssZ')}} {{!-- Sun Dec 31 2017 23:00:00 GMT+0000 --}}
1{{moment-format <date> [outputFormat=moment.defaultFormat [<inputFormat>]]}}
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
1{{moment-format '12-1995-25' 'MM/DD/YYYY' 'MM-YYYY-DD'}} {{!-- 12/25/1995 --}}
1{{moment-from <dateA> [<dateB>] [hideAffix=false]}} 2{{moment-from-now <dateA> [hideAffix=false]}}
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
1{{!-- in January 2018 at time of writing --}} 2{{moment-from '2995-12-25'}} {{!-- in 978 years --}} 3{{moment-from-now '2995-12-25'}} {{!-- in 978 years --}} 4{{moment-from '1995-12-25' '2995-12-25' hideAffix=true}} {{!-- 1000 years --}}
1{{moment-to <dateA> [<dateB>] [hideAffix=false]}} 2{{moment-to-now <dateA> [hideAffix=false]}}
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
1{{!-- in January 2018 at time of writing --}} 2{{moment-to '2995-12-25'}} {{!-- 978 years ago --}} 3{{moment-to '1995-12-25' '2995-12-25'}} {{!-- in 1000 years --}} 4{{moment-to-now '1995-12-25' hideAffix=true}} {{!-- 22 years --}}
1{{moment-duration <number> [<units>]}}
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
1{{moment-duration 100}} {{!-- a few seconds --}} 2{{moment-duration 24 'hours'}} {{!-- a day --}}
1{{moment-calendar <dateA> [<dateB> [<formatHash>]]}}
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
1{{!-- in January 2018 at time of writing --}} 2{{moment-from-now '2018-01-25'}} {{!-- 2 days ago --}} 3{{moment-calendar '2018-01-25'}} {{!-- Yesterday at 12:00 AM --}}
1{{moment-diff <dateA> <dateB> [precision='milliseconds' [float=false]]}}
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
1{{moment-diff '2018-01-25' '2018-01-26'}} {{!-- 86400000 --}} 2{{moment-diff '2018-01-25' '2018-01-26' precision='years' float=true}} {{!-- 0.0026881720430107525 --}}
1{{moment-add <date> <number> [precision='milliseconds']}}
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
1{{!-- Add 6 days to a date --}} 2{{moment-add '10-19-2019' 6 precision='days'}} 3 4{{!-- Add 6 days to a date --}} 5const duration = { days: 6 } 6{{moment-add '10-19-2019' duration}} 7 8{{!-- Print a date 6 days from now --}} 9{{moment-add 6 precision='days'}}
1{{moment-subtract <date> <number> [precision='milliseconds']}}
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
1{{!-- Remove 6 days from a date --}} 2{{moment-subtract '10-19-2019' 6 precision='days'}} 3 4{{!-- Remove 6 days from a date --}} 5const duration = { days: 6 } 6{{moment-subtract '10-19-2019' duration}} 7 8{{!-- Print a date 6 days earlier --}} 9{{moment-subtract 6 precision='days'}}
1{{is-before <dateA> [<dateB>] [precision='milliseconds']}} 2{{is-after <dateA> [<dateB>] [precision='milliseconds']}} 3{{is-same <dateA> [<dateB>] [precision='milliseconds']}} 4{{is-same-or-before <dateA> [<dateB>] [precision='milliseconds']}} 5{{is-same-or-after <dateA> [<dateB>] [precision='milliseconds']}}
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
1{{is-before '2995-12-25'}} {{!-- false --}} 2{{is-before '2018-01-25' '2018-01-26' precision='years'}} {{!-- false --}} 3{{is-same-or-after '2018-01-25' '2018-01-26' precision='years'}} {{!-- true --}}
1{{is-between <date> <dateA> [<dateB>] [precision='year' inclusivity='[)']}}
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
1{{is-between '1995-12-25' '2995-12-25'}} {{!-- true --}} 2{{is-between '1995-12-25' '1995-12-25' '2995-12-25' precision='years' inclusivity='()'}} {{!-- true --}}
1{{now}} 2{{moment-format (now) 'MM-DD-YYYY'}}
Returns the present Moment.
Examples
1{{!-- date at time of writing }} 2{{now}} {{!-- Sat Jan 27 2018 11:59:31 GMT-0500 --}} 3{{!-- interval is a common named parameter (see the corresponding section) }} 4{{now interval=1000}} {{!-- <current date and updating every 1-second (1000 milliseconds).> --}}
1{{unix <timestamp>}}
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
1{{unix '1516586508'}} {{!-- Sun Jan 21 2018 21:01:48 GMT-0500 --}} 2{{!-- Warning: Passing a literal integer value does not work --}} 3{{unix 1516586508}} {{!-- Invalid date --}}
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
1{{now interval=1000}} {{!-- <current date and updating every 1-second (1000 milliseconds)> --}} 2{{is-before (now) '2018-01-26' interval=60000}} {{!-- if this was true initially, it will always be true despite interval --}} 3{{moment-format '' allow-empty=true}} {{!-- <nothing> --}}
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.
1// config/environment.js 2module.exports = function() { 3 return { 4 'ember-moment': { 5 outputFormat: 'L' 6 } 7 } 8};
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.
1// app/controller/index.js 2export default Ember.Controller.extend({ 3 moment: Ember.inject.service(), 4 actions: { 5 changeDefaultFormat() { 6 this.set('moment.defaultFormat', 'MM.DD.YYYY'); 7 } 8 } 9})
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
.
1// config/environment.js 2module.exports = function() { 3 return { 4 'ember-moment': { 5 allowEmpty: true // default: false 6 } 7 } 8};
1// app/routes/applicaton.js 2export default Ember.Route.extend({ 3 moment: Ember.inject.service(), 4 beforeModel() { 5 this.get('moment').setLocale('es'); 6 } 7});
1// app/routes/applicaton.js 2export default Ember.Route.extend({ 3 moment: Ember.inject.service(), 4 beforeModel() { 5 this.get('moment').setTimeZone('America/Los_Angeles'); 6 } 7});
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)
1{{moment-format ''}} {{!-- Invalid date --}} 2{{moment-format '' allow-empty=true}} {{!-- <nothing> --}}
See the Contributing guide for details.
No vulnerabilities found.
No security vulnerabilities found.