flutter_date_formatter 0.0.7
flutter_date_formatter: ^0.0.7 copied to clipboard
A Flutter package for formatting dates and times in various locales. This package provides utilities for formatting relative dates, ordinal numbers, and more.
Flutter Date Formatter #
A Flutter package for formatting dates and times in various locales. This package provides utilities for formatting relative dates, ordinal numbers, and more.
Installation 💻 #
Install via flutter pub add:
dart pub add flutter_date_formatter
Usage 📖 #
Import the package:
import 'package:flutter_date_formatter/flutter_date_formatter.dart';
Prerequisites #
Install the intl package:
dart pub add intl
Add initializeDateFormatting() to the main function and import it from package:intl/date_symbol_data_local.dart:
import 'package:intl/date_symbol_data_local.dart'; // Add this import
void main() async {
await initializeDateFormatting(); // Initialize date formatting
// Your app initialization code here
}
Formatting Dates #
You can format dates using the FlutterDateFormatter class:
DateTime now = DateTime.now();
String pattern = 'do MMMM yyyy';
String formattedDate = FlutterDateFormatter(pattern, 'en').format(now);
print(formattedDate); // Output: 13th February 2025
Formatting Relative Dates #
You can format dates relative to the current time using the formatRelativeDateTime function:
DateTime now = DateTime.now();
DateTime pastDate = now.subtract(Duration(days: 5));
String formattedRelativeDate = FlutterDateFormatter.formatRelativeDateTime(
pastDate,
locale: 'en',
);
print(formattedRelativeDate); // Output: 5 days ago
Ordinal Numbers #
You can get the ordinal representation of a number using the ordinal method in the locale classes:
int number = 1;
String ordinal = FlutterDateFormatter.ordinal(number, locale: 'en');
print(ordinal); // Output: 1st
DateTime Extensions #
The package now includes several new extension methods for the DateTime class:
DateTime date = DateTime.now();
print(date.isToday); // Output: true
String pattern = 'do MMMM yyyy';
String formattedDate = date.format(pattern: pattern, locale: 'en');
print(formattedDate); // Output: 13th February 2025
DateTime pastDate = now.subtract(Duration(days: 5));
String formattedRelativeDate = pastDate.formatRelative(
locale: 'en',
);
print(formattedRelativeDate); // Output: 5 days ago
Properties
isFuture → bool
isPast → bool
isToday → bool
isYesterday → bool
isTomorrow → bool
isLocal → bool
isWeekend → bool
isLeapYear → bool
dayOfWeek → int
dayOfYear → int
weekOfYear → int
quarterOfYear → int
startOfDay → DateTime
startOfWeek → DateTime
startOfMonth → DateTime
startOfYear → DateTime
endOfDay → DateTime
endOfWeek → DateTime
endOfMonth → DateTime
endOfYear → DateTime
Methods
endOf(Unit unit) → DateTime
startOf(Unit unit) → DateTime
subYears(int amount) → DateTime
subMonths(int amount) → DateTime
subWeeks(int amount) → DateTime
subDays(int amount) → DateTime
subHours(int amount) → DateTime
subMinutes(int amount) → DateTime
subSeconds(int amount) → DateTime
subMilliseconds(int amount) → DateTime
subMicroseconds(int amount) → DateTime
addMonths(int amount) → DateTime
addWeeks(int amount) → DateTime
addDays(int amount) → DateTime
addHours(int amount) → DateTime
addMinutes(int amount) → DateTime
addSeconds(int amount) → DateTime
addMilliseconds(int amount) → DateTime
addMicroseconds(int amount) → DateTime
diff( DateTime other, { Unit unit = Unit.microsecond, bool asFloat = false }) → num
isSame(DateTime other, {Unit unit = Unit.microsecond}) → bool
isSameOrBefore(DateTime other, {Unit unit = Unit.microsecond}) → bool
isSameOrAfter(DateTime other, {Unit unit = Unit.microsecond}) → bool
isSameDay(DateTime other) → bool
isSameWeek(DateTime other) → bool
isSameMonth(DateTime other) → bool
isSameYear(DateTime other) → bool
isSameMinute(DateTime other) → bool
isBetween(DateTime startDateTime, DateTime endDateTime, {Unit unit = Unit.microsecond}) → bool
format({String? pattern, String? locale}) → String
formatRelative({String? locale, DateTime? clock, bool allowFromNow = false, bool short = false, bool withPrefixAndSuffix = true}) → String
formatFrom({required DateTime clock, String? locale, bool short = false, bool withPrefixAndSuffix = true}) → String
formatFromNow({String? locale, bool short = false, bool withPrefixAndSuffix = true}) → String
formatTo({required DateTime clock, String? locale, bool short = false, bool withPrefixAndSuffix = true}) → String
formatToNow({String? locale, bool short = false, bool withPrefixAndSuffix = true}) → String
Supported Locales #
The package supports multiple locales. Here is a list of all supported locales:
Formatting & Relative Locales Supported
am- Amharicar- Arabicaz- Azerbaijanibe- Belarusianbn- Bengalibs- Bosnianca- Catalancs- Czechda- Danishde- Germanen- Englishes- Spanishet- Estonianfa- Persianfi- Finnishfr- Frenchhe- Hebrewhi- Hindihr- Croatianhu- Hungarianid- Indonesianit- Italianja- Japaneseka- Georgiankm- Khmerko- Koreanlv- Latvianmn- Mongolianms_my- Malaymy- Burmesenb- Norwegian Bokmålnl- Dutchpl- Polishps- Pashtopt- Portuguesero- Romanianru- Russiansv- Swedishth- Thaitl_PH- Filipinotr- Turkishuk- Ukrainianvi- Vietnamesezh- Chinese
Only Supported Relative Locales
dv- Divehigr- Greekku- Kurdishnn- Norwegian Nynorskrw- Kinyarwandatk- Turkish
Contributing 🤝 #
Contributions are welcome! Please open an issue or submit a pull request.
License 📄 #
This project is licensed under the MIT License - see the LICENSE file for details.