Change dates to relative time format 5/04/2023 12:00am = 9 hours ago

I have put some code together so that now you can display dates in a relative to the current time format. For example. Instead of 05/04/2023 12:00am this code changes it to 9 hours ago for example. It picks up seconds, minutes, hours, days, weeks, months, and years. This works on all lists, tables and detail sections and thiss works everytime the page is loaded.

So it goes like this and theres only 2 things you need to do to get this to work.

  1. You need to have the selected date in the builder set to either DD/MM/YYYY or MM/DD/YYYY and make sure you include the time in am/pm format.
  2. You need add the field number for the date on the second line of the code for the variable relativeDateFields seprated by ,

$(document).on('knack-view-render.any', function (scene) {
var relativeDateFields = $('.field_2, .field_* ');
// Find the 'span' element inside the parent element that includes text with a date
relativeDateFields.find('span').each(function() {
  var spanText = $(this).text();
  var dateRegex = /\d{1,2}\/\d{1,2}\/\d{4}/;

  if (spanText && dateRegex.test(spanText)) {
    $(this).addClass('realtiveDatefield');
  }
});
});

$(document).on('knack-view-render.any', function (scene) {
  // get the date text from the HTML element
  var dateText = $('.realtiveDatefield').text();

  // determine the date format (either "DD/MM/YYYY" or "MM/DD/YYYY")
  var dateFormat = dateText.includes('/') ? 'DD/MM/YYYY' : 'MM/DD/YYYY';

  // split the date and time components
  var dateTime = dateText.split(' ');

  // split the date components into day, month, and year
  var dateParts = dateTime[0].split('/');
  var day, month, year;
  if (dateFormat === 'DD/MM/YYYY') {
    day = parseInt(dateParts[0]);
    month = parseInt(dateParts[1]) - 1; // subtract 1 from the month to get a 0-based index
    year = parseInt(dateParts[2]);
  } else {
    month = parseInt(dateParts[0]) - 1; // subtract 1 from the month to get a 0-based index
    day = parseInt(dateParts[1]);
    year = parseInt(dateParts[2]);
  }

  // split the time components into hours and minutes
  var timeParts = dateTime[1].split(':');
  var hours = parseInt(timeParts[0]);
  var minutes = parseInt(timeParts[1]);

  // create a Date object from the date and time components
  var date = new Date(year, month, day, hours, minutes);

  // calculate the difference between the date and the current time
  var diff = new Date() - date;

  // define time intervals in milliseconds
  var minute = 60 * 1000;
  var hour = 60 * minute;
  var day = 24 * hour;
  var week = 7 * day;
  var month = 30 * day;
  var year = 365 * day;

  // determine which time interval the difference falls within
  var interval;
  if (diff < minute) {
    interval = Math.round(diff / 1000);
    $('.realtiveDatefield').text(interval + ' second' + (interval > 1 ? 's' : '') + ' ago');
  } else if (diff < hour) {
    interval = Math.round(diff / minute);
    $('.realtiveDatefield').text(interval + ' minute' + (interval > 1 ? 's' : '') + ' ago');
  } else if (diff < day) {
    interval = Math.round(diff / hour);
    $('.realtiveDatefield').text(interval + ' hour' + (interval > 1 ? 's' : '') + ' ago');
  } else if (diff < week) {
    interval = Math.round(diff / day);
    $('.realtiveDatefield').text(interval + ' day' + (interval > 1 ? 's' : '') + ' ago');
  } else if (diff < month) {
    interval = Math.round(diff / week);
    $('.realtiveDatefield').text(interval + ' week' + (interval > 1 ? 's' : '') + ' ago');
  } else if (diff < year) {
    interval = Math.round(diff / month);
    $('.realtiveDatefield').text(interval + ' month' + (interval > 1 ? 's' : '') + ' ago');
  } else {
    interval = Math.round(diff / year);
    $('.realtiveDatefield').text(interval + ' year' + (interval > 1 ? 's' : '') + ' ago');
  }
});
3 Likes

@Johnny_Appknowledged_UK

This looks great. We will definitely use this.

Thank you

1 Like

That’s some serious code, looking forward to giving it a test drive. Thanks for sharing :+1:

1 Like

Thanks Carl, If you need any help let me know :grinning :grinning:

Thanks @CSWinnall. If you need any help let me know.

Hey everyone, I hope you’ve been enjoying these code snippets. I wanted to let you know that all of my code is now available on my new platform, Appknowledged. Appknowledged has taken over from KnackMods. I encourage all of you to sign up—it’s completely free—and you’ll have access to all of my free content.