1

I'm trying to use this library http://momentjs.com/ with key MHMchiX6c1bwSqGM1PZiW_PxhMjh3Sh48 How do I use momentsjs in Google Apps Script?

var moment = Moment.load();
var d = moment().format("DD MMMM YYYY");
Logger.log(moment(d).format('LL'));

and I get date in correct format in English, but I need it in Russian. When I add the line:

var moment = Moment.load();
moment.locale('ru');
var d = moment().format("DD MMMM YYYY");
Logger.log(moment(d).format('LL'));

It doesn't work and I get notification - Cannot find function locale in Object function (b, c, d, e) {...}. Is it something wrong in my code?

Community
  • 1
  • 1
  • have you loaded the `ru` locale? http://momentjs.com/docs/#/i18n/changing-locale/ – Jaromanda X Jul 21 '15 at 23:17
  • hmm, says locale isn't a function, perhaps you have an old version of moment.js and need to use `moment.lang('ru');` instead – Jaromanda X Jul 21 '15 at 23:21
  • 1
    Yes, it's seems library use the old version of script... I took the script from the site momentjs.com with locales and this code: moment.locale('ru'); var d = moment().format("DD MMMM YYYY"); Logger.log(moment(d).format('LL')); works fine now! Thank you all for answers =) – Роман Савкив Jul 22 '15 at 07:49

2 Answers2

1

You need the library which contains locales - located here: https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.10.3/moment-with-locales.js Also you have a couple of coding errors:

var d = moment().format("DD MMMM YYYY");

At this point d is a text string - not a moment object. Try this instead:

moment.locale('ru');
var d = moment();
Logger.log(d.format('LL'));

Should work..

But note moment.js by itself doesn't support locales.

nril
  • 558
  • 2
  • 7
1

With latest moment.js (2.18.1) you may do it like this:

function testMomentLocaleRussian() {
  eval(UrlFetchApp.fetch('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.18.1/moment-with-locales.js').getContentText());

  moment.locale('ru');
  var m = moment();
  Logger.log(m.format('LL'));
}

In order to use latest moment.js remove the old library from google app scripts and just use the eval(UrlFetchApp.fetch like my example above. Replace 2.18.1 with latest version.

This link provides all moment.js versions you can use from cdnjs (or host moment.js on your own server):

https://cdnjs.com/libraries/moment.js/

apadana
  • 13,456
  • 15
  • 82
  • 98