timeago-js is a small (<1 kb) Javascript library with multilingual support that automatically updates date elements with user friendly statements, such as "just now", "n seconds ago" etc. It offers mul
-
git clone https://github.com/osmanjtekin/timeago-js.git
-
Add
type="module"
to the script tag, andimport {timeago} from "./helpers/timeago-js/timeago.js"
to the js file.
By default, the timeago()
function:
- Targets all elements with the
time
class. - Retrieves the timestamp value (in seconds) from the
data-timestamp
attribute of the targeted elements. - Sets the refresh rate to 30 seconds
- Sets the language to English
Custom values can be set with: timeago(<class_name>, <refresh_rate>, <language>)
.
To include a new language, add a new array containing the localized string to languages.js:
const <language>_<territory> = [
"<now|one second ago>", "<seconds ago>",
"<one minute ago>", "<minutes ago>",
"<one hour ago>", "<hours ago>",
"<one day ago>", "<days ago>",
"<one week ago>", "<weeks ago>",
"<one month ago>", "<months ago>",
"<one year ago>", "<years ago>"
];
Where:
- language is an ISO 639 language code.
- territory is an ISO 3166 country code. Some examples being
it_IT
,en_GB
,fr_FR
. - odd values are the singular and even values are the plural form
Make sure you add <language>_<territory>
to export.
Then either open an issue or submit a pull request.
This project is licensed under the MIT License - see the LICENSE file for details.