timeago 1.1.1

  • 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.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/timeago.dart';

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

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

  // Preload spanish messages
  await TimeAgo.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
    await TimeAgo.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.1.1

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.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=1.23.0 <2.0.0