How to handle timezones in Parse.com

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.)

moment

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.

Step 2 – Moment Timezone Source Code

Download the latest version of moment-timezone.js and add it to your Parse cloud folder.

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.

6 kommentarer om “How to handle timezones in Parse.com”

  1. 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?

    1. 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.

      1. 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:)

        1. 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.

Legg inn en kommentar