Has anyone created a snippet for formatting a timestamp into 2mins ago, 2hours ago 2days ago etc?

Options

I see from the Xano docs there is "Relative Time Formats" but I can't seem to get them to work, especially the 'ago' one. Any help would be greatly appreciated!

Answers

  • Cameron B
    Cameron B Member, Administrator

    ADMIN

    Options

    Hey, great question. Timestamps and their filters can take a while to get used to. I'd be curious to know how or what you think would make the `time`experience easier.

    No snippet per se, but I can share one with you if you'd like :)

    Otherwise, check this example out. I have var by the name of now, and its value is simply the now timestamp.

    Then, I update this variable.

    I added the transform_timestamp filter and add the '2 days ago'.


    This then returns a timestamp, that when I test on https://www.unixtimestamp.com/ , it outputs:

    Let me know if this is clear or what else I can do :)

  • choulsey
    choulsey Member
    Options

    Hi Cameron, thanks so much for taking the time to reply. Unfortunately, I probably didn't explain myself properly. I was looking to pull the timestamp from the DB and then format it based on how long ago the row was added and then present it to the user. E.g "this post was added 2 days ago". Sorry my explanation was poor. I have since implemented a solution using from if/then logic and maths. Thanks again for replying!

  • Ray Deck
    Ray Deck Trusted Xano Expert ✭✭✭
    Options

    hey @choulsey, this is easiest to do with a lambda. Xano has support for Luxon in its lambdas, which makes localization of those kinds of comparisons very easy, e.g.

    return DateTime.fromMillis($var.compareTime).toRelative()
    

    For example, producing a result like "1 minute ago" or "3 months ago" depending on the compareTime variable value. (I'm assuming compareTIme is a xano timestamp or a number denoting the time in UTC ms)