timeago 2.0.14

  • readme.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 98

timeago #

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

Pub

Build Status

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.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('ja', timeago.JaMessages());
  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('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.NbNoShortMessages());
  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());

  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.14

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';
  
Version Uploaded Documentation Archive
2.0.16 May 16, 2019 Go to the documentation of timeago 2.0.16 Download timeago 2.0.16 archive
2.0.15 May 15, 2019 Go to the documentation of timeago 2.0.15 Download timeago 2.0.15 archive
2.0.14 Apr 12, 2019 Go to the documentation of timeago 2.0.14 Download timeago 2.0.14 archive
2.0.13 Mar 29, 2019 Go to the documentation of timeago 2.0.13 Download timeago 2.0.13 archive
2.0.12 Mar 28, 2019 Go to the documentation of timeago 2.0.12 Download timeago 2.0.12 archive
2.0.11 Mar 27, 2019 Go to the documentation of timeago 2.0.11 Download timeago 2.0.11 archive
2.0.10 Dec 13, 2018 Go to the documentation of timeago 2.0.10 Download timeago 2.0.10 archive
2.0.9 Nov 3, 2018 Go to the documentation of timeago 2.0.9 Download timeago 2.0.9 archive
2.0.8 Sep 11, 2018 Go to the documentation of timeago 2.0.8 Download timeago 2.0.8 archive
2.0.7 Sep 3, 2018 Go to the documentation of timeago 2.0.7 Download timeago 2.0.7 archive

All 36 versions...

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

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

  • Dart: 2.3.1
  • pana: 0.12.16

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.90 points)

577 out of 579 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. (-1 points)

Analysis of lib/src/timeago.dart reported 2 hints:

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

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

Format lib/src/messages/de_messages.dart.

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

Format lib/src/messages/en_messages.dart.

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

Fix additional 8 files with analysis or formatting issues.

Additional issues in the following files:

  • 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/fr_messages.dart (Run dartfmt to format lib/src/messages/fr_messages.dart.)
  • lib/src/messages/ja_messages.dart (Run dartfmt to format lib/src/messages/ja_messages.dart.)
  • lib/src/messages/ko_messages.dart (Run dartfmt to format lib/src/messages/ko_messages.dart.)
  • lib/src/messages/pl_messages.dart (Run dartfmt to format lib/src/messages/pl_messages.dart.)
  • lib/src/messages/ru_messages.dart (Run dartfmt to format lib/src/messages/ru_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 ^0.9.2
build_web_compilers ^0.4.1
test ^1.3.0