Search packages, repositories and app categories

Package detailsv1.1.1

simple_moment

A simple implementation of the Moment.js features for relative time.

AndroidiOSWebmacOSWindowsLinux
30-day downloads
483
Likes
51
Pub points
120

Install package

flutter pub add simple_moment

Package Overview

Package selection guidance compiled from public package information, community data, and project usage.

A lightweight Flutter package that brings essential Moment.js-like functionality for displaying relative time. It simplifies the formatting of time differences, such as '2 minutes ago' or 'in 5 hours', across multiple platforms. Designed for ease of use, it provides a clean API to convert timestamps into human-readable relative strings without heavy dependencies. Ideal for apps needing intuitive time representations in user interfaces.

Use cases

  • Displaying post timestamps
  • Showing message send times
  • Updating live activity indicators
  • Enhancing user notifications
  • Tracking event deadlines

Key features

  • Simple relative time formatting
  • Cross-platform support
  • Minimal dependency footprint
  • Easy integration with DateTime objects
  • No external libraries required

Suitable for

  • Apps with real-time updates
  • Social media interfaces
  • Chat applications
  • Task management tools
  • Any app requiring readable time differences

Considerations

  • Limited to relative time only
  • No date parsing or manipulation
  • Does not support localization
  • Not suitable for complex date operations
  • Requires manual handling of timezone offsets

Original README

Original README snapshot from the linked source repository. Visit the repository for the latest content.

simple_moment Pub Build Status

A simple implementation of the Moment.js features for relative time in Dart.

Usage

A simple usage example:

import 'package:simple_moment/simple_moment.dart';

main() {
    var secondsToAdd = Duration(seconds: 10);
    var dateForComparison = DateTime.now().add(secondsToAdd);
    var moment = Moment.now();

    // should print "in a few seconds"
    print(moment.from(dateForComparison));
    
    Moment rawDate = Moment.parse("2020-07-03");
      // should print "2020-08-03 00:00:00.000
      print(rawDate.add(months: 1));
    
      // should print "2019-07-03 00:00:00.000
      print(rawDate.subtract(years: 1));
    
      //should print "03-07-2020 00:00"
      print(rawDate.format("dd-MM-yyyy HH:mm"));
}

Locales

Set the locale for all usages of Moment:
Moment.setLocaleGlobally(new LocaleDe());
Set the locale only for the current instance of Moment:
var moment = new Moment.now().locale(new LocaleDe());
Adding your own locale:

Just create a class that implements ILocaleData and assign that to your Moment instance or set it globally.

Overwriting existing locales:
class ShortLocaleEn extends LocaleEn {
  String get seconds => '%is';

  String get aMinute => '%im';
  String get minutes => '%im';

  String get anHour => '%ih';
  String get hours => '%ih';

  String get aDay => '%id';
  String get days => '%id';
}
Using intl to format dates
// create new Moment instance with german locale
var moment = Moment.now().locale(LocaleDe(), useInFormat: true);

// initialize intl
await initializeDateFormatting(moment.usedLocale.localeString);

// format and print the current month in german
var formattedString = moment.format("LLLL");
print(formattedString);

Features and bugs

Please file feature requests and bugs at the issue tracker.

Categories

Dependencies

Dependency list 1 items

Source repository

Projects using this package