1

I know it's not recommended to store MomentJS objects in databases (although I don't really understand why). I'm having some bizarre problems storing them: my local MongoDB vs. the remote MongoLab seem to be outputting slightly different date/times (is this because of timezone difference?).


For example:

When I save: moment(1 + '-' + 2014, 'Q-YYYY')

MongoLab: 1388534400000

Local MongoDB: 1388563200000

That difference comes out to 8 hours.


In order to fix this problem, should I convert my Moment objects to ISO strings with .toISOString()? I'm also interested in why this problem happens, if anyone has the answer to that.

sir_thursday
  • 5,270
  • 12
  • 64
  • 118
  • Have you tried setting an explicit timezone? – Chris Chang Aug 22 '14 at 23:23
  • No, I'm not sure how to do that. Isn't that more of a round-about fix, instead of not storing moment objects in the DB in the first place? – sir_thursday Aug 22 '14 at 23:24
  • 1
    It's mostly due to the timezone in your local machine and the TZ in the MongoLab's server. – Enrique Fueyo Aug 22 '14 at 23:25
  • So what's the accepted way of fixing this? Should I have different code on my local v. remote? Or is there a way to standardize the times across timezones? – sir_thursday Aug 22 '14 at 23:25
  • You can standardise on a timezone like GMT and use [`moment.timezone`](http://momentjs.com/timezone/docs/). However, if you only need to store `Q-YYYY` then perhaps you only want to save that string rather than a Date object that has to be re-parsed. – Stennie Aug 25 '14 at 07:10

0 Answers0