timeago 1.2.6

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

timeago

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

Pub

Build Status

Usage

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

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

    print(timeAgo(fifteenAgo)); // 15 minutes ago
}

If you are using it in multiple places and want a little more control you can create your own instance:

import 'package:timeago/timeago.dart';

main() async {
    TimeAgo ta = new TimeAgo();
    final fifteenAgo = new DateTime.now().subtract(new Duration(minutes: 15));
    final fifteenFromNow = new DateTime.now().add(new Duration(minutes: 15));
    
    print(ta.format(fifteenAgo)); // 15 minutes ago
    print(ta.format(fifteenFromNow, until: true)); // 15 minutes from now
    
    // Change locale
    ta.locale = 'es';
    
    print(ta.format(fifteenAgo)); // hace 15 minutos
    print(ta.format(fifteenFromNow, until: true)); // dentro de 15 minutos
    
}

Using on the Browser(lazy load localizations)

While you can use TimeAgo as previously described in the browser it will load all the current localization messages at once. To lazyload localizations use import 'package:timeago/browser_timeago.dart'; instead:

import 'package:timeago/browser_timeago.dart';

main() async {
    TimeAgo ta = new TimeAgo();
    final fifteenAgo = new DateTime.now().subtract(new Duration(minutes: 15));
    final fifteenFromNow = new DateTime.now().add(new Duration(minutes: 15));
    
    print(ta.format(fifteenAgo)); // 15 minutes ago
    print(ta.format(fifteenFromNow, until: true)); // 15 minutes from now
    
    
    // Lazy Load locale messages, only need to do this once per locale.
    await ta.initializeLocale("es");
    
    // Change locale
    ta.locale = 'es';
    
    print(ta.format(fifteenAgo)); // hace 15 minutos
    print(ta.format(fifteenFromNow, until: true)); // dentro de 15 minutos
    
}

Live Demo

Here

Features and bugs

Please file feature requests and bugs at the issue tracker.

Changelog

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

// ignore: implementation_imports
import 'dart:async' show Timer;
import 'dart:html';
import 'package:timeago/browser_timeago.dart';

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

main() async {
  final loadedTime = new DateTime.now();

  // Preload spanish messages
  defaultTimeAgo.initializeLocale('es');

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

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

    // Initialize selected locale
    defaultTimeAgo.initializeLocale(el.text);

    // Set locale for the default time ago object
    defaultTimeAgo.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(currentTime.subtract(new Duration(microseconds: 1 * 44 * 1000))));
  addItem(timeAgo(currentTime.subtract(new Duration(minutes: 1))));
  addItem(timeAgo(currentTime.subtract(new Duration(minutes: 5))));
  addItem(timeAgo(currentTime.subtract(new Duration(minutes: 50))));
  addItem(timeAgo(currentTime.subtract(new Duration(hours: 5))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 1))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 5))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 30))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 30 * 5))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 365))));
  addItem(timeAgo(currentTime.subtract(new Duration(days: 365 * 5))));

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

timeUntil(DateTime date) {
  return timeAgo(date, until: true);
}

Use this package as a library

1. Depend on it

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


dependencies:
  timeago: "^1.2.6"

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
1.2.6 Jun 5, 2018 Go to the documentation of timeago 1.2.6 Download timeago 1.2.6 archive
1.2.5 May 16, 2018 Go to the documentation of timeago 1.2.5 Download timeago 1.2.5 archive
1.2.4 May 1, 2018 Go to the documentation of timeago 1.2.4 Download timeago 1.2.4 archive
1.2.3 Apr 25, 2018 Go to the documentation of timeago 1.2.3 Download timeago 1.2.3 archive
1.2.2 Apr 23, 2018 Go to the documentation of timeago 1.2.2 Download timeago 1.2.2 archive
1.2.1 Feb 20, 2018 Go to the documentation of timeago 1.2.1 Download timeago 1.2.1 archive
1.2.0 Oct 23, 2017 Go to the documentation of timeago 1.2.0 Download timeago 1.2.0 archive
1.1.1 Sep 24, 2017 Go to the documentation of timeago 1.1.1 Download timeago 1.1.1 archive
1.1.0 Sep 24, 2017 Go to the documentation of timeago 1.1.0 Download timeago 1.1.0 archive
1.0.2 Jun 15, 2017 Go to the documentation of timeago 1.0.2 Download timeago 1.0.2 archive

All 17 versions...

Analysis

We analyzed this package on Jun 19, 2018, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.0.0-dev.63.0
  • pana: 0.11.3

Scores

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

Platforms

Detected platforms: Flutter, web, other

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

Suggestions

  • Fix analysis and formatting issues.

    Analysis or formatting checks reported 8 hints.

    Run dartfmt to format lib/src/all_messages.dart.

    Run dartfmt to format lib/src/base_timeago.dart.

    Similar analysis of the following files failed:

    • lib/src/i18/fr_messages.dart (hint)
    • lib/src/i18/ja_messages.dart (hint)
    • lib/src/i18/messages_all.dart (hint)
    • lib/src/i18/zh_CN_messages.dart (hint)
    • lib/src/i18/zh_messages.dart (hint)
    • lib/timeago.dart (hint)

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.23.0 <2.0.0
intl ^0.15.1 0.15.6
Transitive dependencies
path 1.6.1
Dev dependencies
browser any
test ^0.12.22