0.1.5 • Published 1 year ago

timeagoplus v0.1.5

Weekly downloads
-
License
MIT
Repository
github
Last release
1 year ago

timeagoplus

timeagoplus is a WebAssembly (Wasm) package that provides a utility to calculate and display the time elapsed since a given timestamp in a human-readable format. It supports multiple languages.

Features

  • Calculates the time elapsed since a given timestamp.
  • Supports detailed labels (e.g., "1 year, 2 months ago").
  • Multi-language support.

Installation

Install the package via npm:

npm install timeagoplus

Usage

Below are examples of how to use the timeagoplus package in both a React component and a plain JavaScript file.

React Example

First, make sure you have installed the timeagoplus package:

npm install timeagoplus

Now, here's how you can use it in a React component:

import React, { useState, useEffect } from 'react';
import init, { TimeAgo, Language } from 'timeagoplus';

const HomePage: React.FC = () => {
  const [time, setTime] = useState('');

  useEffect(() => {
    let intervalId: NodeJS.Timeout;

    // Initialization (Runs Only Once)
    init().then(() => {
      // Create a TimeAgo instance with default language and detailed label
      const timeAgo = new TimeAgo(Language.TRK);

      // Example using timestamp
      setTime(timeAgo.format(new Date().getTime())); // "now"

      // Update Logic (Runs every second)
      intervalId = setInterval(() => {
        setTime(timeAgo.format(new Date('2024-06-27T20:00:00+03:00')));
      }, 1000); // Update every second
    });

    // Cleanup (Runs when the component unmounts)
    return () => clearInterval(intervalId);
  }, []);

  return <div>{time}</div>;
};

export default HomePage;

JavaScript Example

For plain JavaScript, make sure to also install the timeagoplus package:

npm install timeagoplus

Here's how you can use it in a simple HTML file:

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>TimeAgoPlus Example</title>
  </head>
  <body>
    <div id="time"></div>
    <script type="module">
      import init, { TimeAgo, Language } from 'timeagoplus';

      document.addEventListener('DOMContentLoaded', async () => {
        await init();
        const timeAgo = new TimeAgo(Language.TRK);

        // Example using timestamp
        document.getElementById('time').textContent = timeAgo.format(
          new Date().getTime()
        ); // "now"

        // Update Logic (Runs every second)
        setInterval(() => {
          document.getElementById('time').textContent = timeAgo.format(
            new Date('2024-06-27T20:00:00+03:00')
          );
        }, 1000); // Update every second
      });
    </script>
  </body>
</html>

Both examples initialize the timeagoplus package, create a TimeAgo instance, and set up an interval to update the displayed time every second. The React example uses React hooks, while the JavaScript example uses plain DOM manipulation.

Supported Languages

The Language enum provides a wide range of languages you can use.

Language CodeLanguage Name
ACMMesopotamian Arabic
AECSaidi Arabic
AEBTunisian Arabic
AMHAmharic
APCNorth Levantine Arabic
APDSamar-Leyte Visayan
ARQAlgerian Arabic
ARSNajdi Arabic
ARYMoroccan Arabic
ARZEgyptian Arabic
ASMAssamese
AWDAwadhi
AYNNorthern Altai
AZBSouth Azerbaijani
AZEAzerbaijani
BGCHaryanvi
BHJBhojpuri
BLGBulgarian
BMSJavanese
BNGBengali
CEBCebuano
CFRCrimean Tatar
CHNMandarin Chinese
CJYJinyu Chinese
CITCitak
CCXNorthern Luri
CZCCzech
DCCDakhini
DUTDutch
ENGEnglish
FRNFrench
FUVNigerian Fulfulde
GAZWest Central Oromo
GERGerman
GJRGujarati
GRKGreek, Modern (1453-)
HAKHakka Chinese
HATHaitian; Haitian Creole
HILHiligaynon
HNDHindi
HNEChhattisgarhi
HNGHungarian
HSNXiang Chinese
HUAMandarin Chinese
IGRIgbo
ILOIloko
INZIndonesian
ITNItalian
JANJavanese
JPNJapanese
KAZKazakh
KJVKannada
KKNKokni
KMRKhmer
KNNGan Chinese
KURCentral Kurdish
LMOLombard
MEXMexican Spanish
MJSMalaccan Creole Malay
MKDMacedonian
MKPMohawk
MHJMahajani
MLIMalayalam
MNPManipuri
MQMMacaense
MRTMarathi
NEPNepali
NPLNepali
ORYOdia
PBTSouthern Pashto
PBUNorthern Pashto
PESWestern Farsi
PNJEastern Panjabi
PNBWestern Panjabi
PQLPomeranian
PORPortuguese
PRSEastern Farsi
RUAKinyarwanda
RUMRomanian
RUSRussian
RUWKinyarwanda
SHDKundal Shahi
SKRSaraiki
SNHSinhala
SNDSindhi
SOMSomali
SPNSpanish
SRCSerbian
SUOFinnish
SWDSwedish
TCVCaviteño
TCWTelugu
TGLTagalog
THJTai
TTRTatar
TTSTausug
TRKTurkish
TWSTwi
UKRUkrainian
URDUrdu
UZBUzbek
UIGUighur
VIEVietnamese
WUUWu Chinese
YORYoruba
YUHChinese, Yue
ZUUZulu

License

This project is licensed under the MIT License - see the LICENSE file for details.

Author

Fatih Yavuz

Repository

GitHub


This README includes a brief introduction, installation instructions, usage examples, a description of the time_ago function, supported languages, licensing information, and links to the author and repository.