timeago 2.0.8

  • README.md
  • CHANGELOG.md
  • Example
  • Installing
  • Versions
  • 97

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
}

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

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());

  final loadedTime = new DateTime.now();

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

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

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter packages get

Alternatively, your editor might support pub get or flutter packages 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.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
2.0.6 Sep 3, 2018 Go to the documentation of timeago 2.0.6 Download timeago 2.0.6 archive
2.0.5 Sep 3, 2018 Go to the documentation of timeago 2.0.5 Download timeago 2.0.5 archive
2.0.4 Aug 31, 2018 Go to the documentation of timeago 2.0.4 Download timeago 2.0.4 archive
2.0.3 Aug 28, 2018 Go to the documentation of timeago 2.0.3 Download timeago 2.0.3 archive
2.0.2 Aug 15, 2018 Go to the documentation of timeago 2.0.2 Download timeago 2.0.2 archive
2.0.1 Aug 10, 2018 Go to the documentation of timeago 2.0.1 Download timeago 2.0.1 archive
2.0.0 Aug 10, 2018 Go to the documentation of timeago 2.0.0 Download timeago 2.0.0 archive
1.3.0 Aug 9, 2018 Go to the documentation of timeago 1.3.0 Download timeago 1.3.0 archive

All 28 versions...

Popularity:
Describes how popular the package is relative to other packages. [more]
95
Health:
Code health derived from static analysis. [more]
100
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 Oct 10, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0
  • pana: 0.12.4

Platforms

Detected platforms: Flutter, web, other

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

Health suggestions

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.

Format lib/src/messages/es_messages.dart.

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

Fix additional 5 files with analysis or formatting issues.

Additional issues in the following files:

  • 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/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