Angular – Format date with angular2 moment

angularangular2-momentmomentjs

I'm getting following error, when using amTimeAgo pipe from angular2-moment.

Deprecation warning: value provided is not in a recognized RFC2822 or ISO format.
moment construction falls back to js Date(), which is not reliable across all browsers and versions.
Non RFC2822/ISO date formats are discouraged and will be removed in an upcoming major release.
Please refer to http://momentjs.com/guides/#/warnings/js-date/ for more info.
Arguments:
[0] _isAMomentObject: true, _isUTC: false, _useUTC: false, _l: undefined, _i: 21-03-2017, _f: undefined, _strict: undefined, _locale: [object Object]

Also pipe is printing Invalid date.

I'm using it like this:
<span class="date-created"> {{ job.createdAt | amTimeAgo }} </span>

And value of job.createdAt is string in format: 22-03-2017.

I understand that something is wrong with format, but don't know how to pass that custom format ('DD-MM-YYYY') to pipe, so that moment package and this angular library can recognize it.

Any ideas?

Best Answer

I guess, the string is not being correctly converted to date.. you can try below two options:

{{job.createdAt |  date:'MM/dd/yyyy' | amTimeAgo }}

or convert the string to date in your typescript file:

let newDate = new Date(job.createdAt);
Related Topic