https://github.com/sadespresso/moment_dart
Moment converts Dart DateTime object to human-readable text. And many more benefits
https://github.com/sadespresso/moment_dart
calendar dart dart-package date datetime momentjs time
Last synced: 6 months ago
JSON representation
Moment converts Dart DateTime object to human-readable text. And many more benefits
- Host: GitHub
- URL: https://github.com/sadespresso/moment_dart
- Owner: sadespresso
- License: mit
- Created: 2021-12-25T11:58:54.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-07-21T08:49:58.000Z (11 months ago)
- Last Synced: 2024-07-21T14:58:36.184Z (11 months ago)
- Topics: calendar, dart, dart-package, date, datetime, momentjs, time
- Language: Dart
- Homepage:
- Size: 480 KB
- Stars: 20
- Watchers: 1
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# moment_dart
[](https://pub.dartlang.org/packages/moment_dart)
[](https://opensource.org/licenses/MIT)
[](#contributors)> Inspired by moment.js
## References
### [📖 Read documentation](https://dev.gege.mn/moment_dart_docs)
### [🆕 Migrate to 1.0.0](#migrate-to-100)
## Usage
### Creating instance
There are multiple ways to create an instance.
```dart
// 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`.
```dart
Moment.setGlobalLocalization(MomentLocalizations.fr());
```### Relative Duration
Relative durations are rather inprecise. See [precise duration](#precise-duration)
for accuracy and flexibility.Read about thresholds, and more on [docs](https://dev.gege.mn/moment_dart_docs/duration)
```dart
final yesterday = Moment.now() - const Duration(days: 1);
yesterday.fromNow(); // a day agofinal 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](https://dev.gege.mn/moment_dart_docs/duration/precise).
(delimiter, custom units, abbreviation...)On `Duration` objects:
```dart
Duration(days: 67, hours: 3, minutes: 2).toDurationString(
localization: MomentLocalizations.de(),
form: Abbreviation.semi,
); // in 2 Mo. 7 Tg.
```For `Moment`:
```dart
final epoch = Moment.fromMillisecondsSinceEpoch(0);
final now = DateTime(2023, DateTime.july, 14).toMoment();print(epoch.fromPrecise(now)); // 53 years 7 months ago
```### Calendar
```dart
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](https://dev.gege.mn/moment_dart_docs/extension/)
```dart
final now = DateTime.now(); // July 19 2023 03:12 PM (UTC+8)// ISO week, ISO week year
now.week; // 48
now.weekYear; // 2023now.isLeapYear; // false
// Date creates new `DateTime` with hours, minutes, etc.. omitted
now.date; // July 19 2023 00:00 AMnow.clone(); // July 19 2023 03:12 PM
now.nextTuesday(); // July 25 2023 03:12 PM
// and many more!
```## Migrate to 1.0.0
* Instead of `MomentLocalizations.ko()`, use `MomentLocalizations.koKr()`
* Replace `UnitStringForm` with:
* `UnitStringForm.short` => `Abbreviation.full`
* `UnitStringForm.mid` => `Abbreviation.semi`
* `UnitStringForm.full` => `Abbreviation.none`
* In `Moment().calendar()` calls: remove `weekStart` argument
* `DateTimeConstructors.withTimezone`: move `isUtc` argument to first position
* Replace deprecated methods## Contributing
Contributions of any kind are welcome! Please refer to [CONTRIBUTE.md](CONTRIBUTE.md)
## Contributors