user266003
user266003

Reputation:

Serializing dates in moment.js

I have a calendar which is initialized this way:

  startDate: moment().subtract('month', 1).startOf('month'),
  endDate: moment()
  }, function(start, end) { // on date change - save it and retrieve later
     // start is the start date
     // end is the end date
  }

I want to be able to save and restore the start and end dates. As you can see startDate and endDate must be in "moment". I can store it to a cookie or on the server - that doesn't matter.

My question is how to serialize the dates and then restore?

Upvotes: 5

Views: 5433

Answers (1)

Shawn Jacobson
Shawn Jacobson

Reputation: 1352

Best solution is to store as milliseconds since epoch...

var ms = moment().valueOf();

...then you can easily restore by passing that value into the moment constructor...

var dt = moment(ms);

Upvotes: 17

Related Questions