]> git.ipfire.org Git - thirdparty/moment.git/commitdiff
Fix from/fromNow on invalid moments (fixes #2079)
authorIskren Chernev <iskren.chernev@gmail.com>
Mon, 4 May 2015 07:52:02 +0000 (00:52 -0700)
committerIskren Chernev <iskren.chernev@gmail.com>
Mon, 4 May 2015 07:52:02 +0000 (00:52 -0700)
src/lib/moment/from.js
src/test/moment/locale.js

index 0044eede0addb9347969483b429b77e38db365a1..c6a5449ffac6670f563b72a7a319bd8b3bb68c37 100644 (file)
@@ -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);
 }
 
index 2cab7edbadd0b30775ff4adba83146b703bbd3ff..70247d3c66cb4485ce15205013d2ac6018eae5b6 100644 (file)
@@ -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]);