josh
josh

Reputation: 75

Moment's fromNow returning strange result

I'm using fromNow in Moment and I seem to be getting some odd results.

var moment = require('moment');

var months = {
    Jan: '1', Feb: '2', Mar: '3', Apr: '4',
    May: '5', Jun: '6', Jul: '7', Aug: '8',
    Sep: '9', Oct: '10', Nov: '11', Dec: '12' }

var input = "3 Aug 2015, 12:30".replace(/,/g, "").split(' ')
var time = input.pop().split(':')
var date = input.reverse()
var t = date.concat(time);

t[1] = months[t[1]];
t = moment(t);

console.log(t.fromNow());

This is printing out "in a month".

Upvotes: 0

Views: 137

Answers (1)

Val
Val

Reputation: 217304

It seems like the array you're feeding into moment (i.e. ["2015", "Aug", "3", "12", "30"]) is not a valid date. If you print out t.toDate() after assigning it moment(t) it states "Invalid Date".

Why not simply doing it like this and let moment parse your date properly:

var m = moment("3 Aug 2015, 12:30", "DD MMM YYYY, HH:mm");
console.log(m.fromNow());

> in 5 hours

Upvotes: 2

Related Questions