From: Iskren Chernev Date: Mon, 4 May 2015 07:52:02 +0000 (-0700) Subject: Fix from/fromNow on invalid moments (fixes #2079) X-Git-Tag: 2.10.3~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b0413fc7d0df5e352fd8e744185aa0e2673fca88;p=thirdparty%2Fmoment.git Fix from/fromNow on invalid moments (fixes #2079) --- diff --git a/src/lib/moment/from.js b/src/lib/moment/from.js index 0044eede0..c6a5449ff 100644 --- a/src/lib/moment/from.js +++ b/src/lib/moment/from.js @@ -2,6 +2,9 @@ import { createDuration } from '../duration/create'; import { createLocal } from '../create/local'; export function from (time, withoutSuffix) { + if (!this.isValid()) { + return this.localeData().invalidDate(); + } return createDuration({to: this, from: time}).locale(this.locale()).humanize(!withoutSuffix); } diff --git a/src/test/moment/locale.js b/src/test/moment/locale.js index 2cab7edba..70247d3c6 100644 --- a/src/test/moment/locale.js +++ b/src/test/moment/locale.js @@ -236,6 +236,11 @@ test('duration deprecations', function (assert) { assert.equal(moment.duration().lang(), moment.duration().localeData(), 'duration.lang is the same as duration.localeData'); }); +test('from and fromNow with invalid date', function (assert) { + assert.equal(moment(NaN).from(), 'Invalid Date', 'moment.from with invalid moment'); + assert.equal(moment(NaN).fromNow(), 'Invalid Date', 'moment.fromNow with invalid moment'); +}); + test('from relative time future', function (assert) { var start = moment([2007, 1, 28]);