Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

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

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

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

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.

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

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

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

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

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

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"

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

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

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

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

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

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

Yurii Orishchuk
Posts: 0
Joined: Fri Feb 14, 2014 8:20 am

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

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.

Mark Wong
Posts: 0
Joined: Sat Feb 14, 2015 7:47 am

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

Thank you! It works!

Best Regards.

Louis Adekoya
Posts: 0
Joined: Sun Nov 17, 2013 10:51 pm

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

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();"

Return to “Issues”