dated 1.0.0
dated: ^1.0.0 copied to clipboard
A library provides useful extensions for working with DateTime objects.
Dated #
dated is a Dart package that provides useful extensions for working with DateTime objects. It includes extensions for comparing dates, checking if dates are the same day, month, or year, and calculating the time ago from a given date.
Installation #
Add the dated package to your pubspec.yaml file:
dart pub add dated
Usage #
Import the dated package in your Dart file:
import 'package:dated/dated.dart';
DatedBaseBoolExtension #
The DatedBaseBoolExtension provides methods to compare DateTime objects.
final today = DateTime.now();
final yesterday = today.subtract(const Duration(days: 1));
final nextMonth = today.add(const Duration(days: 30));
print(today.isSameDay(yesterday)); // false
print(today.isSameMonth(nextMonth)); // false
print(today.isSameYear(nextMonth)); // true
print(yesterday.isBeforeDay(today)); // true
print(today.isBeforeMonth(nextMonth)); // true
print(nextMonth.isAfterYear(today)); // false
DatedBaseTimeAgoExtension #
The DatedBaseTimeAgoExtension provides a method to calculate the time ago from a given DateTime object.
final twoHoursAgo = DateTime.now().subtract(const Duration(hours: 2));
final threeDaysAgo = DateTime.now().subtract(const Duration(days: 3));
final sixMonthsAgo = DateTime.now().subtract(const Duration(days: 180));
print(twoHoursAgo.timeAgo()); // 2 hours ago
print(threeDaysAgo.timeAgo()); // 3 days ago
print(sixMonthsAgo.timeAgo()); // 6 months ago
print(threeDaysAgo.timeAgo(prefix: 'Posted')); // Posted 3 days ago
print(sixMonthsAgo.timeAgo(suffix: 'earlier')); // 6 months earlier
print(twoHoursAgo.timeAgo(prefix: 'Created', suffix: 'ago')); // Created 2 hours ago
API Reference #
DatedBaseBoolExtension #
isSameDay(DateTime other): Returnstrueif theDateTimeobjects represent the same day,falseotherwise.isSameMonth(DateTime other): Returnstrueif theDateTimeobjects represent the same month,falseotherwise.isSameYear(DateTime other): Returnstrueif theDateTimeobjects represent the same year,falseotherwise.isBeforeDay(DateTime other): Returnstrueif theDateTimeobject is before the givenotherdate and not on the same day,falseotherwise.isBeforeMonth(DateTime other): Returnstrueif theDateTimeobject is before the givenotherdate and not in the same month,falseotherwise.isBeforeYear(DateTime other): Returnstrueif theDateTimeobject is before the givenotherdate and not in the same year,falseotherwise.isAfterDay(DateTime other): Returnstrueif theDateTimeobject is after the givenotherdate and not on the same day,falseotherwise.isAfterMonth(DateTime other): Returnstrueif theDateTimeobject is after the givenotherdate and not in the same month,falseotherwise.isAfterYear(DateTime other): Returnstrueif theDateTimeobject is after the givenotherdate and not in the same year,falseotherwise.
DatedBaseTimeAgoExtension #
timeAgo({DateTime? compare, String? prefix, String? suffix}): Returns a string representing the time ago from theDateTimeobject. Optionally, you can provide acompareDateTimeto compare against, aprefixstring to prepend to the output, and asuffixstring to append to the output.
Contributing #
Contributions to the dated package are welcome! If you find any issues or have suggestions for improvements, please open an issue or submit a pull request on the GitHub repository.
License #
This package is licensed under the MIT License.