[1.0.0] - Relative time formatter - format relative time

Does 1 year ago or 1 month ago sound better than 1 January 2019, etc? Maybe you prefer seeing 1 second ago instead of 29:59:59? Then this module is for you.
This can also be an extension for my International module.

What does this do?

This calculates the dates between 2 date tables, then determines how many years/month/days. So that if you input 31 December 2019 and 1 January 2020, it’s return in 1 day, and if you input 31 December 2019 and 31 December 2020, it’ll return in 1 year. it supports both past and future relative time format. Unix time are automatically converted to dates.
It supports the following time units:

  • year
  • quarter
  • month
  • week
  • day
  • hour
  • minute
  • second

And in the following language in the module alone:

  • German (de)
  • English (en)
  • Spanish (es)
  • French (fr)
  • Italian (it)
  • Simplified Chinese (zh)
  • Traditional Chinese (zh_Hant)
  • Japanese (ja)
  • Korean (ko)

With plural accounted, and numbers formatted (1,000 years ago instead of 1000 years ago).

Module

You can get it from here: RelativeTimeFormatter.rbxm (8.3 KB)

API

string RelativeTimeFormatter[locale].Format(date date1, date date2, style = “long”, table availableUnits = { “year”, “month”, “week”, “day”, “hour”, “minute”, “second” })
Format the relative time depending on the available units. The style could either be long, short or narrow.

RelativeTimeFormatter.en.Format({ year = 2019, month = 12, day = 31 }, { year = 2020, month = 1, day = 1 }) --> in 1 day
RelativeTimeFormatter.en.Format({ year = 2019, month = 12, day = 31 }, { year = 2020, month = 1, day = 31 }) --> in 1 month
RelativeTimeFormatter.en.Format({ year = 2019, month = 12, day = 31 }, { year = 2020, month = 12, day = 31 }) --> in 1 year

string RelativeTimeFormatter[locale].FormatFromNow(date date, style = “long”, table availableUnits = { “year”, “month”, “week”, “day”, “hour”, “minute”, “second” })
Format the relative time from the date inputted. e.g. if now is 1 January 2000, 00:00:00 and you input {year = 1999, month = 12, day = 31, hour = 23, min = 59, sec = 59}, it’ll format it as 1 second ago.

number, string RelativeTimeFormatter.NumberUnit(date date1, date date2, table availableUnits = { “year”, “month”, “day”, “hour”, “minute”, “second” })
Maybe you want to support this in other languages? Or you prefer yesterday over 1 day ago?
No problem, it returns number, string tuple, so you can bind this with my International module, with that module supporting over 700 locales.

print(intl.RelativeTimeFormat.new('en'):Format(RelativeTimeFormatter.NumberUnit({ year = 2020, month = 1, day = 1 }, { year = 2019, month = 1, day = 1 }))); --> last year
11 Likes

Interesting. I also like the idea! Great idea!