Tutorial: Get Relative Timezone (e.g. "3 days ago")

Options
Overview

This endpoint GETs a time and returns the relative time to NOW (e.g. "5 minutes ago" or "2 weeks ago"). I use it to show when a post was created.

delta is the difference between NOW and a timestamp input in seconds. Format U.

rel_time is the variable to display the relative time.

Note: I'm using the user's timezone. That's why you see the user_id input and GET user record function.
 ┄ Details• If delta < 2 min, display "now"• delta < 120
• rel_time = "now"
• If delta < 1 hour, display "X minutes ago"• delta < 3600
• rel_time = delta / 60 & minutes ago
• If delta < 2 hours, display "An hour and X min ago"• delta < 7,200
• rel_time = delta / 60 & minutes ago• Note: This is kinda tricky in Xano because you can't concatenate a value before and after delta. You have to subtract and hour and use "%d."[1.png]
• If delta < 1 day, display "X hours ago"• delta < 86,400
• rel_time = delta / 3600 & hours ago
• If delta < 2 days, display "yesterday"• delta < 172,800
• rel_time = yesterday
• If delta < 2 weeks, display "X days ago"• delta < 1,209,600
• rel_time = delta / 86,400 & days ago
• If delta < 2 month, display "X weeks ago"• Note: 2 months here means 60 days, not the calendar month.
• delta < 5,184,000
• rel_time = delta / 604,800 & weeks ago
• If delta < 1 year, display "X months ago"• Note: 1 year here means 365 days and 1 month is 30 days.
• delta < 31,536,000
• rel_time = delta / 2,592,000 & months ago
• else display nice-looking date• Date is M j, Y → "Nov 24, 2021"[2.png]


Helpful Screenshots
Entire Function Stack[3.png]
time_seconds variable[4.png]
delta variable[5.png]
standard rel_time calculation[6.png]

Comments

  • Karl Larson
    Options
    So after I made this, I'm not sure how to actually display the relative time on each post in a feed.

    The endpoint gets the posts of users they follow and returns  wall_new. Then it loops through each post to create the relative time variable for the post. But the endpoint returns the wall_new and I'm not sure how to add the relative_time to each post. I don't want to create a new field in the post database and update it every time this runs.
    [CleanShot 2021-11-24 at 12.19.52@2x.jpg]
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    So if you get that expression to purely return the relative time (no object key just the raw text string)... then at the end of your For each loop add an Update Variable

    Existing Var: wall_proof.the_name_of_the_field_to_update
    Value: relative time

    I think that should do it
  • Michael Udinski
    Michael Udinski Administrator

    ADMIN

    Options
    My bad can you actually leave the function response as is

    then,
    Value: var: rel_time.rel_time
  • Karl Larson
    Options
     That worked - thank you! (Please check my note below to make sure I'm not misguiding anyone.)

    For anyone checking out this post in the future, here's what I did.

    1. Add a text field to the Proof table. This is empty and will remain so because I don't want to update the database every time someone looks at their feed.
    2. Update that newly created field with the relative_time variable. Updating that variable does not post to the original record.
    [CleanShot 2021-11-25 at 09.56.07@2x.jpg]