Page 1 of 1

Set _createdAt to "..ago" format by using Moment.js

Posted: Wed Mar 25, 2015 1:43 pm
by Mark Wong

I'm trying to change the _createdAt format to "... ago"
I have put the Moment.js in my app.
I need to list the _createdAt in the required format by putting code like this in the mapping of the label to the createdAt:
codevar a = moment(value.createdAt, "YYYY-MM-DD HH:mm:ss.SSS");
var b = moment();
var c = a.from(b);
Appery('mobilelabel_49').text(c);/code
It have been proved that its working from my experiment.
I'm not sure about how to change all the text of the label (not only the first one)
And "var a" need to get the response of the _createdAt of the service


Set _createdAt to "..ago" format by using Moment.js

Posted: Thu Mar 26, 2015 4:02 am
by Yurii Orishchuk

Hi Mark,

It seems to be correct code.

By code i guess it should be used on the link from "$] to listitem component.

Please specify what is not working.

Regards.


Set _createdAt to "..ago" format by using Moment.js

Posted: Thu Mar 26, 2015 5:56 am
by Mark Wong

There are no errors in the console...
The Label is still in the same _createdAt format, not as "... ago".


Set _createdAt to "..ago" format by using Moment.js

Posted: Thu Mar 26, 2015 7:44 am
by Mark Wong

How do I call Javascript src inside the event "Run Javascript"?
When I use document.write('');
It just appears "Uncaught TypeError: Cannot read property 'indexOf' of undefined"


Set _createdAt to "..ago" format by using Moment.js

Posted: Thu Mar 26, 2015 8:21 am
by Mark Wong

I've used this code also
codesrc="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js&quot/code
But nothing happen


Set _createdAt to "..ago" format by using Moment.js

Posted: Thu Mar 26, 2015 8:40 am
by Mark Wong

Finally,
I solve the first problem by using this code at mapping to list item:
code$.getScript('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js', function()
{var a = moment("2015-03-25 10:28:25.923", "YYYY-MM-DD HH:mm:ss.SSS");
var b = moment();
var c = a.from(b);
Appery('mobilelabel_49').text(c);
});/code
However it only affect the first label with the correct format;

The other code I've used (mapping to label):
code$.getScript('https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.9.0/moment.min.js', function()
{var a = moment(value.createdAt, "YYYY-MM-DD HH:mm:ss.SSS");
var b = moment();
var c = a.from(b);
jQuery(element).text(c);
});
/code
And it change all the formats of the labels but I think it didn't get the value.createdAt because it shows 2015 years ago


Set _createdAt to "..ago" format by using Moment.js

Posted: Fri Mar 27, 2015 4:34 am
by Yurii Orishchuk

Hi Mark,

Please follow these steps:

  1. Add new JS asset.

  2. Populate it with js code from this remote file: https://cdnjs.cloudflare.com/ajax/lib...

  3. Open goal page on "data" tab.

  4. Find your "list/query" service datasource.

  5. Click on "success" mapping.

  6. Delete any link/links to your goal label(where you want to see result).

  7. Make link from "_createdAt" response parameter to your goal label(where you want to see result).

  8. Click JS on goal label.

  9. Populate it with following JS code:

    pre

    var a = moment(value, "YYYY-MM-DD HH:mm:ss.SSS");
    var b = moment();
    var c = a.from(b);
    return c;

    /pre

    Regards.


Set _createdAt to "..ago" format by using Moment.js

Posted: Fri Mar 27, 2015 5:26 am
by Mark Wong

Thank you! It works!

Best Regards.


Set _createdAt to "..ago" format by using Moment.js

Posted: Fri Jul 24, 2015 10:24 pm
by Louis Adekoya

Hi Yurii/Mark,

I followed Yurii's advice above and it mostly works except for one little thing. When I add a record, it immediately shows as added "an hour ago". How can I get it to say added "just now" or similar, and then later "a few minutes ago"?

Thanks.

EDIT: Never mind. I resolved my problem by changing "var b = moment();" to "var b = moment.utc();"