Peter Nunn
Peter Nunn

Reputation: 2302

moment.js and meteor, problems using stored moments

I must be miss understanding how things like moment.js work in meteor. I have an app in which in some places I'm storing a straight up datetime value in the database and then cornverting it to a moment and hence a string in a template helper.

I decided it might be more efficient to store the moment variable directly in the database and skip a conversion step, but I'm having no success converting the retrieved moment value to a string using .fromNow() or .format(). I keep getting undefined errors.

The code I'm trying to use is

 submittedText: function() {
    var thedate = this.added;
    //var theformat = thedate.format("dddd, MMMM Do YYYY, h:mm:ss a");
    debugger;
    //return this.added.format("dddd, MMMM Do YYYY, h:mm:ss a");
    return moment(this.added).fromNow();
  }

In the bebugger, I can see that "thedate" is holding the correct object (with the correct values) but the format() or fromNow()'s just throw errors. What am I missing here?

Thanks

Upvotes: 0

Views: 371

Answers (1)

Wim
Wim

Reputation: 1985

Moment is a Wrapper around a Date object, storing moment object will be the same as storing the date object (date.toString()). From the momentjs docs, the best way to store a moment object is as a ISO-8601 string. You can get the ISO-8601 string from the moment object using toJson.

Upvotes: 3

Related Questions