
So you want to use Moment and Moment Timezone on Parse.com using Cloud Code. The first thing you’ll probably see is that the Moment library is a supported Cloud Code Module. Unfortunately, that version is super old and unsupported by Moment Timezone.
But don’t worry, its actually really easy to use the newest version of Moment and Moment Timezone with these simple changes.
1. First, download the latest version of Moment.js here: http://momentjs.com/. Get the moment.js un-minified file.
2. Second, download the latest version of Moment Timezone here: http://momentjs.com/timezone/. Get the moment-timezone-all-years.js un-minifed file. (it will be named moment-timezone-with-data.js when you download it)
3. Copy both files to your cloud directory. You can put it in a subdirectory if you want such as cloud/moment.
Next we’ll need to make one simple update to the moment-timezone-with-data.js file. Find this block of code at the top of the file:
/*global define*/ if (typeof define === 'function' && define.amd) { define(['moment'], factory); // AMD } else if (typeof exports === 'object') { module.exports = factory(require('moment')); // Node } else { factory(root.moment); // Browser }
You’ll make a one line change.
// existing line module.exports = factory(require('moment')); // Node // change the require statement to the path to your moment file // here's an example module.exports = factory(require('cloud/moment/moment.js')); // Node
Lastly, you’ll need to include this in your cloud/main.js file like so:
// change the require path to the path to your file var moment = require("cloud/moment/moment-timezone-with-data.js");
Thats it. Now you can use Moment Timezone. If you don’t need timezone, just require the moment.js file.
Anthony Montalbano
If it's worth doing, it's worth doing right.
Published on: February 13, 2015
Last modified on: December 8, 2021