A library useful for creating fuzzy timestamps. (e.g. "5 minutes ago")
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.
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
}
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
}
Please file feature requests and bugs at the issue tracker.
String
) to format
functionBREAKING 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
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
intl
dependency to accept <0.15.0example/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());
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);
}
Add this to your package's pubspec.yaml file:
dependencies:
timeago: ^2.0.10
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.
Now in your Dart code, you can use:
import 'package:timeago/timeago.dart';
Version | Uploaded | Documentation | Archive |
---|---|---|---|
2.0.10 | Dec 13, 2018 |
|
|
2.0.9 | Nov 3, 2018 |
|
|
2.0.8 | Sep 11, 2018 |
|
|
2.0.7 | Sep 3, 2018 |
|
|
2.0.6 | Sep 3, 2018 |
|
|
2.0.5 | Sep 3, 2018 |
|
|
2.0.4 | Aug 31, 2018 |
|
|
2.0.3 | Aug 28, 2018 |
|
|
2.0.2 | Aug 15, 2018 |
|
|
2.0.1 | Aug 10, 2018 |
|
|
Popularity:
Describes how popular the package is relative to other packages.
[more]
|
96
|
Health:
Code health derived from static analysis.
[more]
|
99
|
Maintenance:
Reflects how tidy and up-to-date the package is.
[more]
|
100
|
Overall:
Weighted score of the above.
[more]
|
98
|
We analyzed this package on Feb 20, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
Detected platforms: Flutter, web, other
No platform restriction found in primary library
package:timeago/timeago.dart
.
Document public APIs. (-0.86 points)
415 out of 417 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.
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 6 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/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
.)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 |