Convert Epoch Unix Timestamp Into A Human Readable Format – timeago.js

Category: Date & Time , Javascript | May 3, 2022
Author:tekinosman
Views Total:179 views
Official Page:Go to website
Last Update:May 3, 2022
License:MIT

Preview:

Convert Epoch Unix Timestamp Into A Human Readable Format – timeago.js

Description:

Have you ever wanted to display a human-readable date format such as 5 minutes ago, 10 weeks ago etc. on your website? That’s where timeago.js can help.

This tiny (~600byte minified) Javascript library makes it easy to convert a valid Unix Timestamp (Epoch) into a human-readable string. It is easy to localize to match your website language. Let’s get started.

How to use it:

1. Download the package and import the timeago.js as a module.

<script type="module">
  import {timeago} from "./timeago.js"
</script>

2. Initialize the timeago.js.

timeago();

3. Add the CSS class time to the target element and insert the Unix Timestamp into the data-timestamp attribute:

<!-- 1588021695 = Mon Apr 27 2020 21:08:15 GMT+0000 -->
<span class="time" data-timestamp="1588021695"></span>

4. Override the default CSS selector. Default: 30s.

<!-- 1588021695 = Mon Apr 27 2020 21:08:15 GMT+0000 -->
<span class="example" data-timestamp="1588021695"></span>
timeago('example');

5. Customize the refresh rate. Default: 30s.

timeago('example', 60);

6. Localize the human-readable strings.

// languages.js
const it_IT = [
  "adesso",          "secondi fa",
  "un minuto fa",      "minuti fa",
  "un'ora fa",       "ore fa",
  "un giorno fa",      "giorni fa",
  "una settimana fa",    "settimane fa",
  "un mese fa",      "mesi fa",
  "un anno fa",      "anni fa"
];
export {it_IT}
import {it_IT} from "./languages.js";
timeago('example', 60, it_IT);

You Might Be Interested In:


Leave a Reply