timeago 1.0.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 49

timeago #

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

Pub

Build Status

Usage #

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
    
    // There's also a shorthand for a default time ago object
    print(timeAgo(new DateTime.now())); // just a moment ago
    
    
    // Load locale messages, only need to do this once per locale
    await TimeAgo.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

To do #

  • Add more locales

Features and bugs #

Please file feature requests and bugs at the issue tracker.

Changelog #

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:html' as html;
import 'dart:async' show Timer;
import 'package:timeago/timeago.dart';


main() async {
  var mainElement = html.querySelector("#timeAgo") as html.SpanElement;

  await TimeAgo.initializeLocale('es');

  var updateMainElement = () {
    mainElement.text = timeAgo(new DateTime.now());
  };

  new Timer.periodic(new Duration(seconds: 1), (timer) {
    updateMainElement();
  });
  updateMainElement();

  //Other examples
  var otherElm = html.querySelector("#other") as html.UListElement;

  var addToOther = (String str) {
    otherElm.append(new html.LIElement()..text = str);
  };

  addOtherExamples(addToOther);

  html
      .querySelectorAll("a")
      .onClick
      .listen((html.MouseEvent event) async {
    html.Element el = event.target as html.AnchorElement;
    await TimeAgo.initializeLocale(el.text);
    defaultTimeAgo.locale = el.text;
    otherElm.innerHtml = "";
    addOtherExamples(addToOther);
  });
}

addOtherExamples(Function addToOther) {
  var currentTime = new DateTime.now();
  addToOther(
      timeAgo(currentTime.subtract(new Duration(microseconds: 1 * 44 * 1000))));
  addToOther(timeAgo(currentTime.subtract(new Duration(minutes: 1))));
  addToOther(timeAgo(currentTime.subtract(new Duration(minutes: 5))));
  addToOther(timeAgo(currentTime.subtract(new Duration(minutes: 50))));
  addToOther(timeAgo(currentTime.subtract(new Duration(hours: 5))));
  addToOther(timeAgo(currentTime.subtract(new Duration(days: 1))));
  addToOther(timeAgo(currentTime.subtract(new Duration(days: 5))));
  addToOther(timeAgo(currentTime.subtract(new Duration(days: 30))));
  addToOther(timeAgo(currentTime.subtract(new Duration(days: 30 * 5))));
  addToOther(timeAgo(currentTime.subtract(new Duration(days: 365))));
  addToOther(timeAgo(currentTime.subtract(new Duration(days: 365 * 5))));

  addToOther("-");

  addToOther(
      timeUntil(currentTime.add(new Duration(microseconds: 1 * 44 * 1000))));
  addToOther(timeUntil(currentTime.add(new Duration(minutes: 1))));
  addToOther(timeUntil(currentTime.add(new Duration(minutes: 5))));
  addToOther(timeUntil(currentTime.add(new Duration(minutes: 50))));
  addToOther(timeUntil(currentTime.add(new Duration(hours: 5))));
  addToOther(timeUntil(currentTime.add(new Duration(days: 1))));
  addToOther(timeUntil(currentTime.add(new Duration(days: 5))));
  addToOther(timeUntil(currentTime.add(new Duration(days: 30))));
  addToOther(timeUntil(currentTime.add(new Duration(days: 30 * 5))));
  addToOther(timeUntil(currentTime.add(new Duration(days: 365))));
  addToOther(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.0.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

Alternatively, your editor might support 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]
98
Health:
Code health derived from static analysis. [more]
--
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
--
Overall:
Weighted score of the above. [more]
49
Learn more about scoring.

This package version is not analyzed, because it is more than two years old. Check the latest stable version for its analysis.

The package version is not analyzed, because it does not support Dart 2. Until this is resolved, the package will receive a health and maintenance score of 0.

Analysis issues and suggestions

Support Dart 2 in pubspec.yaml.

The SDK constraint in pubspec.yaml doesn't allow the Dart 2.0.0 release. For information about upgrading it to be Dart 2 compatible, please see https://dart.dev/dart-2#migration.