⏱ Simple Alpine.js plugin to display the human-readable distance between a date and now.

Overview

Alpine TimeAgo

An Alpine.js plugin to return the distance between a given date and now in words (like "3 months ago", "about 2 hours ago" or "in about 5 hours").

version Build size JSDelivr GitHub Gitmoji

🚀 Installation

CDN

Include the following <script> tag in the <head> of your document, just before Alpine.

<script src="https://cdn.jsdelivr.net/npm/@marcreichel/alpine-timeago@latest/dist/alpine-timeago.min.js" defer></script>

NPM

npm install @marcreichel/alpine-timeago

Add the x-timeago directive to your project by importing the package before Alpine.js.

import Alpine from 'alpinejs';
import TimeAgo from '@marcreichel/alpine-timeago';

Alpine.plugin(TimeAgo);

window.Alpine = Alpine;
window.Alpine.start();

🪄 Usage

To convert a Date to the human-readable distance from now, add the x-data and x-timeago directives to an element and pass the date (as a Date or a string in ISO format) to the x-timeago directive. The directive will update the output every 30 seconds.

<span x-data="{ date: new Date() }" x-timeago="date"></span>

Under the hood the directive is using formatDistanceToNow from date-fns.

No suffix

If you do not want the "[diff] ago" suffix or "in [diff]" prefix, you can use the x-timeago.pure modifier.

<span x-data="{ date: new Date() }" x-timeago.pure="date"></span>

Other locales

If you are using the npm installation method for this package or the ESM distribution, you can use the TimeAgo.configure() method to provide a different locale from date-fns.

import TimeAgo from '@marcreichel/alpine-timeago';
import { de } from 'date-fns/locale';

Alpine.plugin(TimeAgo.configure({
    locale: de,
}));

📄 License

Copyright (c) 2022 Marc Reichel and contributors.

Licensed under the MIT license, see LICENSE for details.

You might also like...

📦 Alpine JS plugin to extend the functionality of the official $persist plugin

Alpine JS Persist Extended Alpine JS magic method $storage extends the official $persist plugin to help you work with local storage 📦 Example 👀 div

Dec 28, 2022

Break the barrier of your country, language and distance...

Break the barrier of your country, language and distance...

HACK ON 2.0 PROJECT METAFRATIS Break the barrier of your country, language and distance... PROBLEM OUR PROJECT SOLVES With the onset of Covid-19 we ha

Jul 6, 2021

Generate meshes from signed distance functions and constructive solid geometry operations.

Generate meshes from signed distance functions and constructive solid geometry operations.

sdf-csg Generate meshes from signed distance functions and constructive solid geometry operations. This library is heavily based upon Inigo Quilez's 3

Oct 24, 2022

This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my initial appointment date. It doesn't handle rescheduling.

This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my initial appointment date. It doesn't handle rescheduling.

US-visa-appointment-notifier This is just a script I put together to check and notify me via email (MailGun) when there's an earlier date before my in

Jan 4, 2023

Open-source project inspired by the idea of "Long Distance Lamp"

Open-source project inspired by the idea of

Project inspired by the idea of Long Distance Lamp (e.g. Friendship Lamps) for people with no soldering skills.

Mar 19, 2022

↕️ A little Alpine.js plugin to automatically resize a textarea to fit its content.

↕️ Alpine Autosize ↕️ A little Alpine.js plugin to automatically resize a textarea to fit its content. 🚀 Installation CDN Include the following scri

Nov 5, 2022

Little Alpine.js plugin to add a typewriter effect to any HTML element.

Little Alpine.js plugin to add a typewriter effect to any HTML element.

⌨️ Alpine Typewriter ⌨️ An Alpine.js plugin to add a typewriter effect to any HTML element. 🚀 Installation CDN Include the following script tag in

Dec 28, 2022

A simple library for handling keyboard shortcuts with Alpine.js.

✨ Help support the maintenance of this package by sponsoring me. Alpine.js Mousetrap A simple library for handling keyboard shortcuts with Alpine.js.

Nov 14, 2022

⛲ Sort import declarations into a pleasing and readable cascade.

⛲ eslint-plugin-cascading-imports This plugin allows to automatically enforce a visual "cascading" order for import declarations. Imports in each bloc

Jan 20, 2022
Comments
  • Remove

    Remove "less than", "about" prefixes?

    Hi!

    I see there's an option to remove the "ago" suffix, but I don't see how to remove the prefixes for "less than", "about", etc.

    In some cases I find this handy to produce shorter strings, so for example:

    "less than 20 seconds ago" -> "20 seconds ago" "less than a minute ago" -> "a minute ago" "about 20 hours ago" -> "20 hours ago"

    enhancement 
    opened by jaimeiniesta 2
  • Option to set locale when cdn option is used

    Option to set locale when cdn option is used

    I saw that it is possible to set the user language when using npm but how about setting it dynamically?

    I am using your lib with the cdn and have the following scenario in my mind.

    Scenario: The homepage offers multiple language and the user decides to switch from German to English.

    Is there a way to accomplish this?

    opened by hendrikschneider 2
  • How to deal with time zones?

    How to deal with time zones?

    I am using this plugin to show a refer to a datetime stored in a database. My database stores the datetime in UTC time, which causes issues for users in different timezones.

    For instance, those in Continental Europe will see a timestamp of now as "two hours ago".

    Is there a way to have the plugin use UTC time rather than the user's local time?

    opened by hankhank10 2
Releases(1.4.3)
  • 1.4.3(Dec 22, 2022)

  • 1.4.2(Dec 22, 2022)

  • 1.4.1(Dec 22, 2022)

    What's Changed

    • Feat/typescript by @marcreichel in https://github.com/marcreichel/alpine-timeago/pull/9
    • 👷 Add prettier CI pipeline by @marcreichel in https://github.com/marcreichel/alpine-timeago/pull/10

    Full Changelog: https://github.com/marcreichel/alpine-timeago/compare/1.4.0...1.4.1

    Source code(tar.gz)
    Source code(zip)
  • 1.4.0(Oct 4, 2022)

    What's Changed

    • Add strict mode by @marcreichel in https://github.com/marcreichel/alpine-timeago/pull/7

    Full Changelog: https://github.com/marcreichel/alpine-timeago/compare/1.3.0...1.4.0

    Source code(tar.gz)
    Source code(zip)
  • 1.3.0(Jul 6, 2022)

    What's Changed

    • ✨ Add magic function by @marcreichel in https://github.com/marcreichel/alpine-timeago/pull/5

    Full Changelog: https://github.com/marcreichel/alpine-timeago/compare/1.2.1...1.3.0

    Source code(tar.gz)
    Source code(zip)
  • 1.2.1(Jun 22, 2022)

    What's Changed

    • ✨ Add include seconds option by @marcreichel in https://github.com/marcreichel/alpine-timeago/pull/4

    Full Changelog: https://github.com/marcreichel/alpine-timeago/compare/1.1.0...1.2.1

    Source code(tar.gz)
    Source code(zip)
  • 1.2.0(Jun 22, 2022)

    What's Changed

    • ✨ Add include seconds option by @marcreichel in https://github.com/marcreichel/alpine-timeago/pull/4

    Full Changelog: https://github.com/marcreichel/alpine-timeago/compare/1.1.0...1.2.0

    Source code(tar.gz)
    Source code(zip)
  • 1.1.0(Mar 20, 2022)

    What's Changed

    • ✨ Make suffix configurable via modifier by @marcreichel in https://github.com/marcreichel/alpine-timeago/pull/1

    Full Changelog: https://github.com/marcreichel/alpine-timeago/compare/0.1.0...1.1.0

    Source code(tar.gz)
    Source code(zip)
  • 1.0.0(Mar 20, 2022)

  • 0.1.0(Mar 20, 2022)

Owner
Marc Reichel
Hi! 👋🏼 My name is Marc. I love to code and make the web look beautiful wherever I can.
Marc Reichel
this is a single-page web application. we built a book website where the user can add , remove and display books. we used modules to implement these functionalities. also, we used the Date class to display the date and time.

Awsome Books In this Project, we have built A Books websites. Built With ?? HTML CSS javascript Git & Github Live Demo Here you can find the live Demo

Nedjwa Bouraiou 10 Aug 3, 2022
Send encrypted and decrypted messages with verifiable keys and human readable names.

zooko-msg Encrypt and decrypt messages using AES with a preshared ECDH key generated using keys associated with Handshake names. I noticed that there

Publius Federalist 31 Jul 27, 2022
Enhanced interval features for Node.js, such as promisified interval and human readable time parsing.

Interval-next Interval-next is a package that extends Javascript's built-in setInterval() capabilities. You have a plain and promisified interval meth

Snowy 5 Jul 28, 2022
Convert JSON to human readable HTML

json.human.js: Json Formatting for Human Beings A small library to convert a JSON object into a human readable HTML representation that is easy to sty

Mariano Guerra 955 Dec 3, 2022
parses human-readable strings for JavaScript's Temporal API

?? temporal-parse What is the temporal-parse? Temporal is the next generation of JavaScript's standard Date API. It's currently proposed to TC39 (see:

Eser Ozvataf 22 Jan 2, 2023
Nepali Multi Date Picker for jQuery. Supports both single date selections and multiple date selection.

Nepali Multi Date Picker A simple yet powerful date picker based in Nepali calendar. Supports both single date selections and multiple date selection.

Sanil Shakya 4 May 23, 2022
A clean integration between Cleave.js and Alpine. ✨

✨ Help support the maintenance of this package by sponsoring me. Alpine Mask This packages provide a custom x-mask directive and $mask magic variable,

Ryan Chandler 48 Dec 26, 2022
This restaurant project is a SPA (single-page application) website. The user can navigate between the home, menu and contact page. I used the MealDB API to display some menu items.

Fresh Cuisine This restaurant project is from the Odin Project and it is a SPA (single-page application) website. The user can navigate between the ho

Virag Kormoczy 7 Nov 2, 2022
A concise collection of classes for PHP, Python, JavaScript and Ruby to calculate great circle distance, bearing, and destination from geographic coordinates

GreatCircle A set of three functions, useful in geographical calculations of different sorts. Available for PHP, Python, Javascript and Ruby. Live dem

null 72 Sep 30, 2022
A public board for all the Computer Society and Students to display their profile. An online year-book for you to display your profile in the most creative manner

Student's Yearbook by IEEE Computer Society Student's yearbook is an open-source project which intends to dispaly the students who will be graduating

IEEE Computer Society 11 Dec 18, 2022