timeago 2.0.22

  • Readme
  • Changelog
  • Example
  • Installing
  • 97

timeago #

A library useful for creating fuzzy timestamps. (e.g. "5 minutes ago")

Pub

Build Status

support

About v2 #

  • v2 of this library does not depend on package intl anymore, It didn't make sense for the small amount of text and now we can use the same code for browser, Flutter and server.

  • API is now a lot simpler, no need to create instance just use the format method.

  • Allows to add and override locales messages with setLocaleMessages

  • All v1 locale messages are available to use, currently library only loads by default en, en_short, es and es_short. To add the remaining just call setLocaleMessages, see example.

Usage #

The easiest way to use this library via top-level function timeAgo(date):

import 'package:timeago/timeago.dart' as timeago;

main() {
    final fifteenAgo = new DateTime.now().subtract(new Duration(minutes: 15));

    print(timeago.format(fifteenAgo)); // 15 minutes ago
    print(timeago.format(fifteenAgo, locale: 'en_short')); // 15m
    print(timeago.format(fifteenAgo, locale: 'es')); // hace 15 minutos
}

Adding new locales #

main() {
    final fifteenAgo = new DateTime.now().subtract(new Duration(minutes: 15));

    // Add a new locale messages
    timeago.setLocaleMessages('fr', timeago.FrMessages());

    // Override a locale message
    timeago.setLocaleMessages('en', CustomMessages());

    print(timeago.format(fifteenAgo)); // 15 min ago
    print(timeago.format(fifteenAgo, locale: 'fr')); // environ 15 minutes
}

Live Demo #

Here

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

2.0.22 #

  • Improvements to Polish messages @Patryk27
  • Adds French short messages @woprandi

2.0.21 #

  • Improvements to Zh messages @kj415j45
  • Adds Sv messages @simoneddeland

2.0.20 #

  • Improvements to Es messages @anonymousJacker

2.0.19 #

  • Added Romanian support @stefancandrea
  • Added Italian short version @quantosapplications
  • Improvements to Ja messages @kesoji
  • Improvements to brazilian portuguese @elder004

2.0.18 #

  • Improvements to Ku messages thanks to @Xoshbin

2.0.17 #

  • Improvements to Russian thanks to @Goolpe
  • Add Khmer language thanks to @SuuSoJeat
  • Add Nynorsk language thanks to @arnemolland

2.0.16 #

  • Add Vietnamese locale thanks to @luantranminh
  • Fix nl message thanks to @soowa

2.0.15 #

  • Add Tamil locale thanks to @Purus
  • Fix Arabic wording thanks to @salehahmedZ
  • Fix Portuguese prefix and suffix thanks to @DoodlesEpic

2.0.14 #

  • Add Korean locale thanks to @jaceshim

2.0.13 #

  • Fixes in japanese locale thanks to @mitsuoka

2.0.12 #

  • Add Arabic locale thanks to @ammaratef45
  • Add Kurdish locale thanks to @Xoshbin

2.0.11 #

  • Add Thai locale thanks to @thawatchai
  • Add Norwegian BokmÃ¥l locale thanks to @danibjor
  • Update example fix main timeago text to update with the selected locale

2.0.10 #

  • Add short German locale thanks to @sdolski
  • Export lookupmessages.dart to prevent analyzer warnings

2.0.9 #

  • Add Polish locale thanks to @bartektartanus

2.0.8 #

  • Add Turkish thanks to @ferdigokdemir
  • Add Indonesian thanks to @skybur

2.0.7 #

  • Add danish thanks to @nikolajskov

2.0.6 #

  • Fix zh_cn_messages thanks to @zjinys

2.0.5 #

  • Fix plural day in german translation

2.0.4 #

  • Fix plural days in pt_BR thanks to @leocavalcante

2.0.3 #

  • Adds missing return type (String) to format function

2.0.2 #

  • Adds Nl translation thanks to @Elvander

2.0.1 #

  • Adds missing Fa, It and Ru Locales from merge conflict

2.0.0 #

BREAKING CHANGES:

  • v2 of this library does not depend on package intl anymore, It didn't make sense for the small amount of text and now we can use the same code for browser, Flutter and server.

  • API is now a lot simpler, no need to create instance just use the format method.

  • Allows to add and override locales messages with setLocaleMessages

  • All v1 locale messages are available to use, currently library only loads by default en, en_short, es and es_short. To add the remaining just call setLocaleMessages, see example.

  • See test and example folder file for examples

1.3.0 #

  • Dart 2.0 support

1.2.6 #

  • Fixes to German translation

1.2.6 #

  • Adds Russian translation thanks to @asstet

1.2.5 #

  • Adds Italian translation thanks to @nicofossa

1.2.4 #

  • Adds Farsi translation thanks to @siavash-mohseni

1.2.3 #

  • Fixes Portuguese not working in non deferred import

1.2.2 #

  • Adds Portuguese translation thanks to @jeanpimentel

1.2.1 #

  • Adds German translation thanks to @btastic

1.2.0 #

  • Split import declaration into browser(lazy load) and server.

For browser(lazy load) use: import 'package:timeago/browser_timeago.dart'; For Flutter/server use: import 'package:timeago/browser_timeago.dart';

Breaking: TimeAgo.initializeLocale is no longer a static method

1.1.0 #

  • Adds Chinese and Simplified Chinese thanks to @oraclebox

1.0.2 #

  • Update intl dependency

1.0.0 #

  • API change for better clarity

0.1.2 #

  • Increase intl dependency to accept <0.15.0

0.1.1 #

  • Add fr and ja locales

0.1.0 #

  • Update messages to be more concise

0.0.1 #

  • Add en, en_short, es, and es_short locales

example/main.dart

import 'dart:async' show Timer;
import 'dart:html';
import 'package:timeago/timeago.dart' as timeago;

final mainContainer = querySelector("#main");
final listContainer = querySelector("#list");

var locale = 'en';

main() async {
  timeago.setLocaleMessages('de', timeago.DeMessages());
  timeago.setLocaleMessages('fr', timeago.FrMessages());
  timeago.setLocaleMessages('fr_short', timeago.FrShortMessages());
  timeago.setLocaleMessages('ja', timeago.JaMessages());
  timeago.setLocaleMessages('km', timeago.KmMessages());
  timeago.setLocaleMessages('km_short', timeago.KmShortMessages());
  timeago.setLocaleMessages('id', timeago.IdMessages());
  timeago.setLocaleMessages('pt_BR', timeago.PtBrMessages());
  timeago.setLocaleMessages('pt_BR_short', timeago.PtBrShortMessages());
  timeago.setLocaleMessages('zh_CN', timeago.ZhCnMessages());
  timeago.setLocaleMessages('zh', timeago.ZhMessages());
  timeago.setLocaleMessages('it', timeago.ItMessages());
  timeago.setLocaleMessages('it_short', timeago.ItShortMessages());
  timeago.setLocaleMessages('fa', timeago.FaMessages());
  timeago.setLocaleMessages('ru', timeago.RuMessages());
  timeago.setLocaleMessages('tr', timeago.TrMessages());
  timeago.setLocaleMessages('pl', timeago.PlMessages());
  timeago.setLocaleMessages('th', timeago.ThMessages());
  timeago.setLocaleMessages('th_short', timeago.ThShortMessages());
  timeago.setLocaleMessages('nb_NO', timeago.NbNoMessages());
  timeago.setLocaleMessages('nb_NO_short', timeago.NbNoShortMessages());
  timeago.setLocaleMessages('nn_NO', timeago.NnNoMessages());
  timeago.setLocaleMessages('nn_NO_short', timeago.NnNoShortMessages());
  timeago.setLocaleMessages('ku', timeago.KuMessages());
  timeago.setLocaleMessages('ku_short', timeago.KuShortMessages());
  timeago.setLocaleMessages('ar', timeago.ArMessages());
  timeago.setLocaleMessages('ar_short', timeago.ArShortMessages());
  timeago.setLocaleMessages('ko', timeago.KoMessages());
  timeago.setLocaleMessages('vi', timeago.ViMessages());
  timeago.setLocaleMessages('vi_short', timeago.ViShortMessages());
  timeago.setLocaleMessages('ta', timeago.TaMessages());
  timeago.setLocaleMessages('ro', timeago.RoMessages());
  timeago.setLocaleMessages('ro_short', timeago.RoShortMessages());
  timeago.setLocaleMessages('sv', timeago.SvMessages());
  timeago.setLocaleMessages('sv_short', timeago.SvShortMessages());

  final loadedTime = new DateTime.now();

  final updateMainContainer = () {
    final now = new DateTime.now();
    final difference = now.difference(loadedTime);
    mainContainer.text = timeago.format(now.subtract(difference), locale: locale);
  };

  querySelectorAll(".locale-link").onClick.listen((event) async {
    final el = event.target as AnchorElement;

    locale = el.text;

    // Recreate list items
    listContainer.innerHtml = "";
    createListItems();
  });

  updateMainContainer();
  createListItems();

  new Timer.periodic(new Duration(seconds: 1), (_) => updateMainContainer());
}

addItem(String text) {
  listContainer.append(new LIElement()..text = text);
}

createListItems() {
  final currentTime = new DateTime.now();
  addItem(
      timeago.format(currentTime.subtract(new Duration(microseconds: 1 * 44 * 1000)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(minutes: 1)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(minutes: 5)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(minutes: 50)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(hours: 5)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(days: 1)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(days: 5)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(days: 30)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(days: 30 * 5)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(days: 365)), locale: locale));
  addItem(timeago.format(currentTime.subtract(new Duration(days: 365 * 5)), locale: locale));

  addItem("-");

  addItem(
      timeUntil(currentTime.add(new Duration(microseconds: 1 * 44 * 1000))));
  addItem(timeUntil(currentTime.add(new Duration(minutes: 1))));
  addItem(timeUntil(currentTime.add(new Duration(minutes: 5))));
  addItem(timeUntil(currentTime.add(new Duration(minutes: 50))));
  addItem(timeUntil(currentTime.add(new Duration(hours: 5))));
  addItem(timeUntil(currentTime.add(new Duration(days: 1))));
  addItem(timeUntil(currentTime.add(new Duration(days: 5))));
  addItem(timeUntil(currentTime.add(new Duration(days: 30))));
  addItem(timeUntil(currentTime.add(new Duration(days: 30 * 5))));
  addItem(timeUntil(currentTime.add(new Duration(days: 365))));
  addItem(timeUntil(currentTime.add(new Duration(days: 365 * 5))));
}

String timeUntil(DateTime date) {
  return timeago.format(date, locale: locale, allowFromNow: true);
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  timeago: ^2.0.22

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:timeago/timeago.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
99
Health:
Code health derived from static analysis. [more]
94
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
97
Learn more about scoring.

We analyzed this package on Nov 15, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.6.0
  • pana: 0.12.21

Platforms

Detected platforms: Flutter, web, other

No platform restriction found in primary library package:timeago/timeago.dart.

Health issues and suggestions

Document public APIs. (-0.93 points)

811 out of 813 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/timeago.dart. (-5.36 points)

Analysis of lib/src/timeago.dart reported 11 hints, including:

line 65 col 5: DO use curly braces for all flow control structures.

line 67 col 5: DO use curly braces for all flow control structures.

line 69 col 5: DO use curly braces for all flow control structures.

line 71 col 5: DO use curly braces for all flow control structures.

line 73 col 5: DO use curly braces for all flow control structures.

Format lib/src/messages/ar_messages.dart.

Run dartfmt to format lib/src/messages/ar_messages.dart.

Format lib/src/messages/de_messages.dart.

Run dartfmt to format lib/src/messages/de_messages.dart.

Fix additional 9 files with analysis or formatting issues.

Additional issues in the following files:

  • lib/src/messages/en_messages.dart (Run dartfmt to format lib/src/messages/en_messages.dart.)
  • lib/src/messages/es_messages.dart (Run dartfmt to format lib/src/messages/es_messages.dart.)
  • lib/src/messages/fa_messages.dart (Run dartfmt to format lib/src/messages/fa_messages.dart.)
  • lib/src/messages/km_messages.dart (Run dartfmt to format lib/src/messages/km_messages.dart.)
  • lib/src/messages/ko_messages.dart (Run dartfmt to format lib/src/messages/ko_messages.dart.)
  • lib/src/messages/ro_messages.dart (Run dartfmt to format lib/src/messages/ro_messages.dart.)
  • lib/src/messages/ru_messages.dart (Run dartfmt to format lib/src/messages/ru_messages.dart.)
  • lib/src/messages/sv_messages.dart (Run dartfmt to format lib/src/messages/sv_messages.dart.)
  • lib/src/messages/zh_messages.dart (Run dartfmt to format lib/src/messages/zh_messages.dart.)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.0.0-dev <3.0.0
Dev dependencies
build_runner ^1.7.0
build_web_compilers ^2.3.0
test ^1.3.0