timeago - Documentation

Introduction

What is timeago?

Timeago is a lightweight JavaScript library that converts timestamps into user-friendly relative time phrases, such as “a few seconds ago,” “3 hours ago,” or “2 days ago.” It provides a simple and elegant way to display dates and times in a more human-readable format, improving the user experience by making time information easily understandable at a glance. This is particularly useful for displaying recently updated content, news feeds, or activity logs.

Key Features

Installation

Timeago can be integrated into your project using various methods:

1. Using a CDN:

The easiest way is to include the library via a CDN. Add the following <script> tag to your HTML file’s <head> section, replacing [version] with the desired version number (check the project’s website for the latest version):

<script src="https://cdnjs.cloudflare.com/ajax/libs/timeago.js/[version]/timeago.min.js"></script>

2. Using npm:

If you’re using npm (Node Package Manager), you can install timeago via the command line:

npm install timeago.js

Then, you can import it into your JavaScript file:

import timeago from 'timeago.js';

3. Using yarn:

Similar to npm, if you’re using yarn, you can install it with:

yarn add timeago.js

And import it into your JavaScript file:

import timeago from 'timeago.js';

After installation (using any of the above methods), you’ll be ready to use the Timeago API in your application. Refer to the API documentation for specific usage instructions.

Basic Usage

Rendering Timeago in HTML

The simplest way to use Timeago is by adding a data-timeago attribute to your HTML element containing a timestamp. Timeago will automatically detect and process these elements when the page loads. The timestamp should be in a format that JavaScript’s Date.parse() function can understand (e.g., ISO 8601 format, or a Unix timestamp).

<time datetime="2024-03-08T12:00:00Z" data-timeago></time>

This will render as a relative time phrase like “a few seconds ago” (if the current time is close to the timestamp) or “3 months ago,” etc., automatically updating periodically.

Using the timeago Function

For more programmatic control, use the timeago function directly. This allows you to format timestamps in your JavaScript code and inject the resulting relative time strings into your HTML.

import timeago from 'timeago.js';

const timestamp = new Date(); // Or a specific timestamp: new Date('2024-03-08T12:00:00Z');
const relativeTime = timeago.format(timestamp);

// Inject the relative time into an HTML element
const element = document.getElementById('myElement');
element.textContent = relativeTime;

This approach gives you greater flexibility, especially when dealing with dynamically generated content or needing more complex integration. Remember to replace 'myElement' with the ID of your target HTML element.

Supported Languages

Timeago offers support for a wide range of languages. To use a language other than the default (usually English), you need to specify the locale using the locale option.

import timeago from 'timeago.js';

const relativeTime = timeago.format(timestamp, 'es'); // 'es' for Spanish, 'fr' for French, etc.

Consult the Timeago documentation or the library’s source code for a complete list of supported locales. You can also refer to the languages folder within the source code.

Custom Language Support

If your desired language isn’t supported, you can add custom language support by creating a new locale file. The structure of this file should follow the pattern established in the existing locale files. Essentially, you need to define a function that maps time differences to appropriate language-specific strings. The library’s documentation or example locale files provide detailed instructions on the file structure and required functions. After adding your custom locale, you can use it just like any other supported language:

import timeago from 'timeago.js';
// Assuming you've added your custom locale 'myLang'
const relativeTime = timeago.format(timestamp, 'myLang');

Remember to include the new locale file in your project and adjust your importing mechanism accordingly.

Configuration Options

Locale Settings

Timeago allows you to easily change the language used for relative time phrases. This is done by setting the locale option. The locale code should correspond to the language file included in or added to the library. For example:

import timeago from 'timeago.js';

timeago.render(document.querySelectorAll('time'), { locale: 'es' }); // For Spanish

If no locale is specified, the default locale (usually English) is used. To see the available locales, refer to the Timeago’s documentation or examine the available language files within the library’s source code. You can also easily add support for your own languages, as described in the Custom Language Support section.

Custom Units

By default, Timeago uses standard units like seconds, minutes, hours, days, weeks, months, and years. You can customize this by providing a customUnits object, which should map unit names to functions that return the number of milliseconds in that unit.

import timeago from 'timeago.js';

const customUnits = {
  fortnight: () => 14 * 24 * 60 * 60 * 1000 // milliseconds in a fortnight
};

timeago.format(timestamp, null, { customUnits });

This example adds a “fortnight” unit to the available units. This approach enables you to include units tailored to your specific application requirements.

Time Formatting

While Timeago automatically handles the relative time phrasing, you can further customize the output format by overriding the functions within each language’s locale file. This offers granular control over the exact phrasing used for different time differences. To fully control the formatting, you would need to modify or create a custom language file with modified functions. Consult the example locale files for guidance on the structure and available functions within the locale object.

Refresh Interval

By default, Timeago updates the displayed relative time periodically to reflect the passage of time. You can control the frequency of these updates using the refreshMillis option. This option sets the interval in milliseconds between updates. Setting it to 0 disables automatic updates.

import timeago from 'timeago.js';

timeago.render(document.querySelectorAll('time'), { refreshMillis: 60000 }); // Update every 60 seconds (1 minute)

Setting a refresh interval is crucial for balancing the accuracy of the relative time displayed and resource usage. Adjust this value according to your application’s needs. A shorter interval provides more up-to-date information but uses more resources. A longer interval conserves resources but may show less up-to-date information.

Advanced Usage

Working with Dates

Timeago accepts various date formats as input. While it generally works well with ISO 8601 formatted strings and Unix timestamps (milliseconds since the epoch), you can use any date object that JavaScript’s Date.parse() function can handle. However, for optimal reliability and consistency, it’s best practice to use ISO 8601 format whenever possible, as it avoids ambiguities associated with other date representations. If you are working with a string representation of a date, ensure it’s in a format parsable by the Date object. If parsing fails, the library might not produce the expected result.

import timeago from 'timeago.js';

// Using ISO 8601 string
let isoDateString = "2024-10-27T10:30:00Z";
let relativeTime = timeago.format(isoDateString);

//Using a Javascript Date object
let jsDate = new Date();
relativeTime = timeago.format(jsDate);


//Using a Unix timestamp (milliseconds)
let unixTimestamp = 1703750600000; // Example timestamp
relativeTime = timeago.format(unixTimestamp);

console.log(relativeTime);

Remember to handle potential errors during date parsing in your application.

Handling Different Time Zones

Timeago primarily works with the user’s local time zone. The timestamp you provide to the timeago function is interpreted as a UTC timestamp unless it explicitly includes timezone information (e.g., using the Z suffix for UTC or specifying an offset such as ‘+02:00’). If your application needs to handle different time zones consistently, ensure you provide timestamps with explicit timezone information according to the standards used within your application (e.g., consistently using UTC for all timestamps). The library itself doesn’t directly handle time zone conversions; that responsibility lies with your application’s back-end or data processing logic.

Integration with Frameworks (React, Angular, Vue)

Timeago’s integration with popular JavaScript frameworks like React, Angular, and Vue typically involves using the library within the component’s lifecycle methods or directives.

The specific implementation will depend on your framework’s conventions and your project’s structure. In most cases, you’ll need to obtain the timestamp (potentially from props or component state) and then use timeago.format() to render the relative time.

Server-Side Rendering

For server-side rendering (SSR), you have several options:

  1. Pre-rendering: Calculate the relative time on the server before sending the HTML to the client. This approach is simple but requires updating the relative time on the client-side after the page loads (to account for the time elapsed since rendering).

  2. Client-side rendering: Include the Timeago library in your client-side code, and render the relative time using JavaScript after the page has loaded on the client. This avoids the need for server-side time calculations but increases the load time slightly.

  3. Hybrid approach: Perform a partial server-side rendering, including a placeholder or initial relative time, and then updating the time on the client side with JavaScript after the full page load to enhance the user experience with minimal initial load time.

The best approach depends on your application’s architecture, performance requirements, and complexity. Consider factors such as the frequency of content updates and the potential latency involved in client-side updates when making your decision.

Troubleshooting

Common Errors

Debugging Tips

Known Issues

Timeago is a relatively stable library, but like any software, it might have some edge cases or limitations. Refer to the project’s issue tracker or documentation for a list of known issues and their potential workarounds. The project’s issue tracker or Github repository (if applicable) will often contain information about known bugs, reported problems and updates to the library. It is always best practice to check the official repository for the most recent information on bug fixes and potential issues that are actively being addressed.

API Reference

timeago Function Details

The core of the Timeago library is the timeago.format() function. It takes a timestamp and optionally a locale code and configuration object as arguments.

Syntax:

timeago.format(timestamp, [locale], [options])

Parameters:

Return Value:

A string representing the relative time phrase. For example, “a few seconds ago”, “3 hours ago”, or “2 days ago”.

Language Locale Objects

Each language supported by Timeago is represented by a locale object. These objects are typically defined in separate files (e.g., en.js, es.js, fr.js) within the Timeago library or in custom locales. The structure of these objects usually involves functions to format relative time, each handling a specific time range (e.g., seconds, minutes, hours, days, etc.). For details on the exact structure and functions, refer to the provided locale files included with the library or in your installation. They typically have a structure such as (this can differ slightly between versions):

{
  numbers: {
      // ...
  },
  past: function (number, suffix) {
      // Function to format past relative times
  },
  future: function(number, suffix) {
      // Function to format future relative times
  },
  // ... other functions and properties
}

The exact keys and functions will depend on the language and the version of the Timeago library you are using.

Configuration Options Details

The options object allows further customization of the timeago.format() function:

const customUnits = {
  fortnight: () => 14 * 24 * 60 * 60 * 1000, // milliseconds in a fortnight
  decade: () => 10 * 365 * 24 * 60 * 60 * 1000 // milliseconds in a decade
};
const options = { refreshMillis: 60000 }; // Update every minute

These options are applied only to the specific timeago.format() call they are passed to. They do not globally affect the library’s behavior for all subsequent calls unless used in conjunction with other API calls that apply the options more widely (such as timeago.render()). Consult the documentation for your specific version of the Timeago library for details on broader configuration settings if available.

Contributing

We welcome contributions to Timeago! Whether it’s fixing bugs, adding new features, improving documentation, or translating to new languages, your help is valuable. Here’s how you can contribute:

Setting up the Development Environment

  1. Fork the repository: Fork the official Timeago repository on GitHub to your own account.

  2. Clone your fork: Clone your forked repository to your local machine using Git:

    git clone <your_fork_url>
  3. Install dependencies: Navigate to the cloned directory and install the project’s dependencies using npm or yarn:

    npm install  // or yarn install
  4. Run the development server (optional but recommended): Many Timeago implementations include a development server for testing purposes, often using tools like Webpack or Parcel. Refer to the project’s README file or documentation for instructions on starting the development server, if applicable. This allows you to see changes reflected in real time.

Coding Style Guide

Follow the existing coding style used in the Timeago project. This usually involves consistent indentation, naming conventions, and commenting practices. Pay attention to the existing codebase for guidance. Consistency in code style will make review much smoother and help maintain the overall quality of the project.

Testing

Timeago likely uses a testing framework (e.g., Jest, Mocha). Before submitting a pull request, ensure your changes are thoroughly tested. Run the existing test suite to verify that your changes haven’t introduced regressions. Add new tests for any new functionality you’ve implemented. Detailed instructions on running the tests are usually found in the project’s README or in a dedicated testing section of the documentation.

Submitting Pull Requests

  1. Create a new branch: Create a new branch for your changes. Use a descriptive name that reflects the purpose of your changes (e.g., fix-bug-123, add-feature-xyz).

  2. Commit your changes: Make your changes, commit them with clear and concise messages explaining what you’ve done.

  3. Push your branch: Push your branch to your forked repository on GitHub:

    git push origin <your_branch_name>
  4. Create a pull request: On GitHub, create a pull request from your branch to the main branch (usually main or master) of the official Timeago repository.

  5. Address feedback: Be responsive to any feedback provided by the maintainers. Address any issues or suggested improvements they raise.

Remember to follow the project’s contribution guidelines and code of conduct found within the repository. Properly following these guidelines greatly increases the chance that your contribution will be accepted and improve the library for others.