6

I'm looking for a way to format dates nicely. It looks like moment.js will give me what I want, but I can't work out how to use it from QML or a Javascript file (this is for Ubuntu Touch).

I've downloaded the with-langs file from the website (and renamed it to 'moment.js'), and then tried "import 'moment.js' as moment" and "Qt.include('moment.js')", but still can't seem to use it.

Any ideas on how to make this work?

Sam Bull
  • 2,559
  • 1
  • 15
  • 17
  • `import "moment.js" as Moment` seems to be right. Can you test it and provide more information what is going wrong? See http://qt-project.org/doc/qt-5.0/qtqml/qtqml-javascript-imports.html for more info. – Kakadu Nov 20 '13 at 08:47
  • It seems to import fine. But, doing anything like 'Moment()' or 'Moment.moment()' seems to fail, I can't find any way to get it working after import. I'm told QML doesn't support all Javascript functions, so maybe it just doesn't work? – Sam Bull Nov 27 '13 at 21:39

1 Answers1

0

I have tried it on my machine and it seems that I have found a problem. QML gives me a parsing error. Indeed, in snippet

(function () {
  extend(null, {
    gett : function (units) {
        return null;
    },

    as : function (units) {
    }
  });
})();

It can't parse code around as because as is a keyword in QML and isn't in javascript. So, it seems that sometimes you can't use javascript libraries "as is".

To fix the problem we can add quotes around as: "as".

Kakadu
  • 2,837
  • 19
  • 29
  • Doesn't seem to have changed anything. It still imports, but I can't find any way to use it. How are you importing and using it? Like: .import "moment.js" as Moment; Moment.moment() Or...? – Sam Bull Nov 28 '13 at 22:23
  • I do `import "moment.js" as Moment` and get some parsing errors on console. – Kakadu Nov 29 '13 at 09:13
  • I don't get parsing errors with or without the change. I just don't see any way to actually use it. Such as running `Moment.moment()`. – Sam Bull Nov 29 '13 at 18:47
  • Annoyingly, someone posted a link to their own blog, where they had it working perfectly. When I asked for them to repeat the code in their answer instead of just posting a link, in order for me to accept the answer, it appears they deleted their answer. I moved away from QML, so I never used the code myself, and don't have the link anymore. So, unfortunately, all I can say is that it is possible, but I have absolutely no recollection of how. – Sam Bull Jun 11 '20 at 16:15