PricingBlog

Relative time - custom formula

  • datomnz-1201306811890929744

    Tom Wrench

    1 year ago

    Hey everyone, so I'm wanting to take a date from string (e.g. "2023-12-04T01:05:21.140365+00:00") and output a relative time. Such as "3 weeks ago" or "2 days ago" or "3 hours ago", depending on how long in the past it was. Then outputting it to a variable in my component. Something similar to the below.

    I've tried creating a custom formula but just can't get it working (custom code is still pretty fresh to me so I'm probably doing something silly).

    function timeAgo(dateString) {
    const date = new Date(dateString).getTime();
    const now = new Date().getTime();
    const diffMs = now - date;
    const diffSecs = Math.floor(diffMs / 1000);
    const diffMins = Math.floor(diffSecs / 60);
    const diffHours = Math.floor(diffMins / 60);
    const diffDays = Math.floor(diffHours / 24);

    if (diffDays > 0) {
    return
    ${diffDays} days ago;
    } else if (diffHours > 0) {
    return
    ${diffHours} hours ago;
    } else if (diffMins > 0) {
    return
    ${diffMins} minutes ago;
    } else {
    return
    ${diffSecs} seconds ago;
    }
    }


    Any ideas?
  • lucasg-1201310357218676796

    Lucas G

    1 year ago

    Looking at it, the literals wouldn't work
  • They need the backticks
  • stockton_f-1201310577826467840

    Stockton

    1 year ago

    AI wrote this, haven’t tried it yet. function timeAgo(timestamp){
    var currentDate = new Date();
    var inputDate = new Date(timestamp);

    var diffSec = Math.floor((currentDate - inputDate) / 1000);

    if (diffSec < 0) return 'in the future';

    if (diffSec < 60) return 'just now';

    var diffMin = Math.floor(diffSec/60);
    if (diffMin < 60) return diffMin + ' minutes ago';

    var diffHrs = Math.floor(diffSec/3600);
    if (diffHrs < 24) return diffHrs + ' hours ago';

    var diffDays = Math.floor(diffSec/86400);
    if (diffDays < 30) return diffDays + ' days ago';

    var diffMons = Math.floor(diffSec/2592000);
    if (diffMons < 12) return diffMons + ' months ago';

    var diffYears = Math.floor(diffSec/31104000);
    return diffYears + ' years ago';
    }

    console.log(timeAgo('2023-12-04T01:05:21.140365+00:00')); // test
  • lucasg-1201313843880669254

    Lucas G

    1 year ago

    @Tom Wrench add the backticks for the literals and tweak the argument
  • function timeAgo(dateString) {
    const date = new Date(args.dateString).getTime();
    const now = new Date().getTime();
    const diffMs = now - date;
    const diffSecs = Math.floor(diffMs / 1000);
    const diffMins = Math.floor(diffSecs / 60);
    const diffHours = Math.floor(diffMins / 60);
    const diffDays = Math.floor(diffHours / 24);

    if (diffDays > 0) {
    return `${diffDays} days ago`;
    } else if (diffHours > 0) {
    return `${diffHours} hours ago`;
    } else if (diffMins > 0) {
    return `${diffMins} minutes ago`;
    } else {
    return `${diffSecs} seconds ago`;
    }
    }
  • Oh I see, Discord removes the literals because of the code markdown haha
  • Nevermind then, you probably had them already
  • Probably just need to adjust the first date const
  • lucasg-1201314795664719882

    Lucas G

    1 year ago

    Yeah, it works:
  • 1201314798604931243-image.png
  • lucasg-1201316211552690236

    Lucas G

    1 year ago

    Would it benefit from adding months and years?
  • datomnz-1201317339963080755

    Tom Wrench

    1 year ago

    Awesome, thank you. I'm probably being a numpty, but how the heck were you able to expose it in your formula panel? It simply doesn't exist for me...

    And yes, months and years would be a sensible inclusion too 🙂
    1201317339774329012-image.png
  • lucasg-1201317540887003288

    Lucas G

    1 year ago

    Create it as a formula, not as a custom action
  • lucasg-1201317947386368040

    Lucas G

    1 year ago

    It should be accessible as a global formula
  • It's part of the reason why I made that other dateFilter formula I shared a while back
  • datomnz-1201318751719661618

    Tom Wrench

    1 year ago

    Of course! As @Tom Ireland would say, every day's a school day. Thanks Lucas - got it working 🙏
    🔥1
  • lucasg-1201318806115594260

    Lucas G

    1 year ago

    It is indeed
  • I will probably save this formula too so thank you for sharing it lol
  • datomnz-1201319017579827331

    Tom Wrench

    1 year ago

    might add in the weeks/months/years to finalise
  • lucasg-1201319062525976596

    Lucas G

    1 year ago

    Great
  • Please share it if you do
  • datomnz-1201328336861925437

    Tom Wrench

    1 year ago

    Added weeks/months/years, plus also adding the correct singular/plural logic for the units (so e.g. it would be "1 week" rather than "1 weeks".

    function timeAgo(dateString) {
    const date = new Date(args.dateString).getTime();
    const now = new Date().getTime();
    const diffMs = now - date;
    const diffSecs = Math.floor(diffMs / 1000);
    const diffMins = Math.floor(diffSecs / 60);
    const diffHours = Math.floor(diffMins / 60);
    const diffDays = Math.floor(diffHours / 24);
    const diffWeeks = Math.floor(diffDays / 7);
    const diffMonths = Math.floor(diffDays / 30); // Approximation
    const diffYears = Math.floor(diffDays / 365); // Approximation

    if (diffYears > 0) {
    return
    ${diffYears} ${diffYears === 1 ? 'year' : 'years'} ago;
    } else if (diffMonths > 0) {
    return
    ${diffMonths} ${diffMonths === 1 ? 'month' : 'months'} ago;
    } else if (diffWeeks > 0) {
    return
    ${diffWeeks} ${diffWeeks === 1 ? 'week' : 'weeks'} ago;
    } else if (diffDays > 0) {
    return
    ${diffDays} ${diffDays === 1 ? 'day' : 'days'} ago;
    } else if (diffHours > 0) {
    return
    ${diffHours} ${diffHours === 1 ? 'hour' : 'hours'} ago;
    } else if (diffMins > 0) {
    return
    ${diffMins} ${diffMins === 1 ? 'minute' : 'minutes'} ago;
    } else {
    return
    ${diffSecs} ${diffSecs === 1 ? 'second' : 'seconds'} ago;
    }
    }
    🔥2
  • lucasg-1201329162833641472

    Lucas G

    1 year ago

    To anybody copying that code, it doesn't have the backticks for the template literals
  • Since formatting seems to remove them
  • But thank you for posting the updated version @Tom Wrench 🙏