In this blog, I will explain how to change the SharePoint date format into TimesAgo.

Example
  • 1 minute ago
  • 1 hour ago
  • 1 day ago
  • 1 month ago
  • 1 year ago
First, read the last modified date value from SharePointList.
  1. var listItems;
  2. var ModifiedDate;
  1. ExecuteOrDelayUntilScriptLoaded(function () {
  2. var ctx = SP.ClientContext.get_current();
  3. var web = ctx.get_web();
  4. var lists = web.get_lists();
  5. var list = lists.getByTitle("CustomList");
  6. ctx.load(list, "LastItemModifiedDate");
  7. var query = new SP.CamlQuery();
  8. query.set_viewXml("");
  9. listItems = list.getItemById('1');
  10. ctx.load(listItems);
  11. ctx.executeQueryAsync(
  12. function () {
  13. if(listItems.get_count() > 0)
  14. {
  15. ModifiedDate= latestItem.get_item("Modified");
  16. }
  17. },
  18. function () {
  19. }
  20. );
  21. }, "sp.js");
  1. var timesago=getTimesago(ModifiedDate);
Call this re-usable JavaScript Function.
  1. function getTimesago(date) {
  2. var seconds = Math.floor((new Date() - date) / 1000);
  3. var interval = Math.floor(seconds / 31536000);
  4. if (interval > 1) {
  5. return interval + " years";
  6. }
  7. interval = Math.floor(seconds / 2592000);
  8. if (interval > 1) {
  9. return interval + " months";
  10. }
  11. interval = Math.floor(seconds / 86400);
  12. if (interval > 1) {
  13. return interval + " days";
  14. }
  15. interval = Math.floor(seconds / 3600);
  16. if (interval > 1) {
  17. return interval + " hours";
  18. }
  19. interval = Math.floor(seconds / 60);
  20. if (interval > 1) {
  21. return interval + " minutes";
  22. }
  23. return Math.floor(seconds) + " seconds";
  24. } alert(timesago)
You will get the output in expected format.

Reference

http://stackoverflow.com/questions/3177836/how-to-format-time-since-xxx-e-g-4-minutes-ago-similar-to-stack-exchange-site
Thanks for reading!!!