Unfortunately Parse is closing down in 2017, but we have found a great and actually local (to us) service: AppStax.
Update: It seems the tutorial might not be valid any more as there are new versions of Parse and Moment. I do not have time to update this turorial as of now (March 2015).
Ever needed to handle dates and time zones in Parse?
(Jump straight to the full Gist example if you want to skip the explanation.)
You might have started with JavaScript Date and your own little hacks. Then realized there is a great js library supported by Parse, as a cloud module, named Moment that makes the hacks a little less hacky. Then you stumble upon Moment Timezone, an add-on to Moment that will solve all your problems. Moment Timezone helps you add timezone information to your moments
. Instead of you needing to know and provide the offset information, you simply give Moment Timezone the timezone in the following format "Europe/Oslo
«.
However Moment Timezone is not supported by Parse! And to make matters a little more complicated Moment Timezone is not compatible with the Moment version on Parse, and the Moment Timezone website has a link to an outdated version of its source code.
With help from Tom Erik I managed to get it all working late last night and maybe it can be of some help to you!
Step 1 – Moment Source Code
Download the latest version of moment.js
and add it to your Parse cloud folder.
- Latest release can be found at Moment’s GitHub.
- I used release 2.5.1: direct link to
moment.js
Step 2 – Moment Timezone Source Code
Download the latest version of moment-timezone.js
and add it to your Parse cloud folder.
- Latest release can be found at Moment Timezone’s GitHub.
- I used release 0.0.3: direct link to
moment-timezone.js
- Change
require('moment')
torequire('cloud/moment.js')
. In release 0.0.3 this will be Line 511.
Step 3 – Timezone Data
Generate JSON data for the timezones you need to support at the Moment Timezone website.
- Parse does not deploy json files, instead create moment-timezone-data.js and expose the json as a js object:
module.export =
.
Step 4 – Use it!
var moment = require('cloud/moment-timezone.js');
moment.tz.add(require('cloud/moment-timezone-data.js'));
Full example
See all files and a full cloud code example in this Gist.
Please let me know if you found this helpful or if something is not working as it should!
Unfortunately Parse is closing down in 2017, but we have found a great and actually local (to us) service: AppStax.
I followed your example but can’t get it to work. I get this error:
TypeError: Cannot call method 'rule' of undefined at Object.ZoneSet.zoneAndRule (moment-timezone.js:308:24) at Object.ZoneSet.offset (moment-timezone.js:322:28) at Function.moment.updateOffset (moment-timezone.js:438:21) at Moment.moment.fn.tz (moment-timezone.js:459:13) at Function.moment.tz (moment-timezone.js:489:6) at main.js:39:31
Do you have any idea what could be wrong?
Thank you so much for letting me know!
I had forgotten
require
at line 4 in main.js, so it added a string – not the json timezone data 😉The gist is updated and should now be working.
Am still getting same error as above: TypeError: Cannot call method ‘rule’ of undefined at…….
i used same code from gist as below:
—
var moment = require(‘cloud/moment-timezone.js’);
moment.tz.add(require(‘cloud/moment-timezone-data.js’));
— Thanks i will be really helpfull if u fix this:)
It has been a while since I did this so this might not work with newer versions of parse and/or moment. The tutorial mentioned by alino91 might help.
The JSON data is too complicated for me 🙁
How can I get data for American timezones?
Unfortunately the source code linked to this post is out-of-date (does not work anymore with the last version of Parse cloud code and moment js and moment timezone) plus the tutorial is incomplete and confusing. For all of you who will read this post, please follow this link (which is a condensed and clear tutorial for setting-up moment js + moment timezone on Parse Cloud Code): http://buildwebthings.com/use-moment-moment-timezone-parse-cloud-code/