Current time/date - Creation time/date = time rounded to closest single unit (hour, day, week, month or year)

Hi all. Can you please tell me how to display time of publishing so that it rounds to closest single unit (hour, day, week, month or year).

Current time/date - Creation time/date = time rounded to closest single unit (hour, day, week, month or year)

here are several examples just to make sure we are on the same page: 1 day ago, 1 week ago, 2 weeks ago, month ago, 8 month ago, 3 years ago… something like this

Thanks

The basis will be similar to this …

https://buildingonbubble.com/block/formatted-countdown-1471178176900x842421891177381900

Convert the difference into seconds.

And instead of using modulo do …

seconds / 60 / 60 :rounded to 0 for hours

seconds / 60 / 60 / 24 :rounded to 0 for days

etc

Then use a conditional to show the various units.

Hi Nigel,

When you say ‘etc’ do you mean I can also use this method for Artur’s other 3 examples of weeks, months, and years?

I’ve managed to do this for hours and days as per above but can’t work out your method for the other 3 time formats.

If you have time, can you also give an example of a conditional as well eg. if less than 1 day show ‘hours’ text if more than 2 weeks show ‘weeks’ text.

Jess

Hi there,

Yes, I think it would be the same for rounding to higher units as well.

Fairly certain there was another thread about this, it is a fairly common requirement. Will have a search.

For others trying to find an answer, I have attached screenshots of Nigel’s suggestion for seconds/minutes/hours/days ago here.

However, you can’t use this really in production as you don’t want something to display ‘568 days ago’ as it would be more sensible as weeks or years.

The first screenshot shows the normal set up in hours and the second screenshot shows the different displays of minutes or days depending on how old the thing is.

Timeago

3 Likes