Search packages, repositories and app categories

Package detailsv5.3.4

moment_dart

Multi-purpose immutable DateTime subclass. Supports multiple localizations to easily convert DateTime and Duration into human-readable format

AndroidiOSWebmacOSWindowsLinux
30-day downloads
6.8K
Likes
86
Pub points
160

Install package

flutter pub add moment_dart

Package Overview

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

moment_dart is a multi-purpose, immutable DateTime subclass designed for easy date and time manipulation in Flutter applications. It supports multiple localizations, enabling developers to convert DateTime and Duration objects into human-readable formats tailored to different languages and regions. Built with immutability in mind, it ensures safe and predictable state management. The package integrates seamlessly across Android, iOS, Web, macOS, Windows, and Linux platforms, making it ideal for cross-platform apps requiring robust date formatting and localization features.

Use cases

  • Formatting dates in multiple languages
  • Calculating time differences with readable output
  • Building localized calendar interfaces
  • Handling timezone-aware date operations
  • Creating dynamic time ago strings
  • Simplifying duration display in UIs

Key features

  • Immutable DateTime replacement
  • Built-in localization support
  • Human-readable time formatting
  • Cross-platform compatibility
  • Duration-to-string conversion
  • Chainable and fluent API

Suitable for

  • Internationalized Flutter apps
  • Apps needing time ago displays
  • Calendar and scheduling tools
  • Localization-heavy projects
  • Developers seeking moment.js-like functionality in Dart

Considerations

  • Requires careful handling of timezones
  • May increase bundle size slightly
  • Limited to date/time formatting use cases
  • Not suitable for complex date arithmetic
  • Depends on external localization data
  • Best paired with intl package for full i18n support

Original README

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

moment_dart

pub package License: MIT All Contributors

Inspired by moment.js

References

📖 Read documentation
🆕 Migrate to 5.x.x

Usage

Creating instance

There are multiple ways to create an instance.

// From DateTime
final a = DateTime.now().toMoment();

final b = Moment(DateTime(...));

// This instance won't be affected by global localization!
final c = Moment(b, localization: MomentLocalizations.koKr());
Setting global localization

By default, the global localization is en_US.

Moment.setGlobalLocalization(MomentLocalizations.fr());
Relative Duration

Relative durations are rather inprecise. See precise duration for accuracy and flexibility.

Read about thresholds, and more on docs

final yesterday = Moment.now() - const Duration(days: 1);
yesterday.fromNow(); // a day ago

final add5sec = Moment.now().add(const Duration(seconds: 5));
add5sec.fromNow(dropPrefixOrSuffix: true); // a few seconds
Precise duration

You can create localized human-readable duration text with abbreviation, highly-customizable units.

See more on docs. (delimiter, custom units, abbreviation...)

On Duration objects:

Duration(days: 67, hours: 3, minutes: 2).toDurationString(
  localization: MomentLocalizations.de(),
  form: Abbreviation.semi,
); // in 2 Mo. 7 Tg.

For Moment:

final epoch = Moment.fromMillisecondsSinceEpoch(0);
final now = DateTime(2023, DateTime.july, 14).toMoment();

print(epoch.fromPrecise(now)); // 53 years 7 months ago
Calendar
final Moment now = Moment.now();

print(now.calendar()); // Today at 03:00 PM
Other helpful methods

moment_dart provides numerous convenience methods. Works on both Moment and DateTime.

See all available options in the docs

final now = DateTime.now(); // July 19 2023 03:12 PM (UTC+8)

// ISO week, ISO week year
now.week; // 48
now.weekYear; // 2023

now.isLeapYear; // false

// Date creates new `DateTime` with hours, minutes, etc.. omitted
now.date; // July 19 2023 00:00 AM

now.clone(); // July 19 2023 03:12 PM

now.nextTuesday(); // July 25 2023 03:12 PM

// and many more!

Contributing

Contributions of any kind are welcome! Please refer to CONTRIBUTE.md

Contributors

Батмэнд Ганбаатар
Батмэнд Ганбаатар

💻 🚧 🌍 📖
Ultrate
Ultrate

💻 🌍
Muhammadyusuf
Muhammadyusuf

🌍 💻
Paweł
Paweł

🌍 💻

Categories

Source repository

Projects using this package