flutter_date_formatter 0.0.9 copy "flutter_date_formatter: ^0.0.9" to clipboard
flutter_date_formatter: ^0.0.9 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 #

pub package

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
startOfQuarter β†’ DateTime
endOfDay β†’ DateTime
endOfWeek β†’ DateTime
endOfMonth β†’ DateTime
endOfYear β†’ DateTime
endOfQuarter β†’ DateTime

Methods

clone() β†’ DateTime
indexOfClosestDay(Iterable<DateTime> dates) β†’ int
closestDayTo(Iterable<DateTime> dates) β†’ DateTime?
isWithin(DateTime start, DateTime end) β†’ bool
isWithinTimeSpan(TimeSpan timeSpan) β†’ bool
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
diffInYears(DateTime other, {bool asFloat = false}) β†’ num
diffInMonths(DateTime other, {bool asFloat = false}) β†’ num
diffInWeeks(DateTime other, {bool asFloat = false}) β†’ num
diffInHours(DateTime other, {bool asFloat = false}) β†’ num
diffInMinutes(DateTime other, {bool asFloat = false}) β†’ num
diffInSeconds(DateTime other, {bool asFloat = false}) β†’ num
diffInMilliseconds(DateTime other, {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
formatOrdinalNumber({String? locale}) β†’ String

Register Custom Locales (Add or Override) #

You can add or override custom locales in the supported locales list:

void main() async {
  await initializeDateFormatting();
  // Adding custom locale
  SupportedLocalesUtils.registerLocale('vi_custom', ViLocaleCustom());

  // Overriding locale
  SupportedLocalesUtils.registerLocale('vi', ViLocaleCustom());
}

class ViLocaleCustom extends Locale {
  @override
  String code() => 'vi_custom';

  @override
  String ordinal(int n) => '';

  @override
  String ordinalNumber(int n) => 'ngΓ y thα»© $n';

  // Custom relative time
  @override
  RelativeDateTime relativeDateTime() => ViCustomRelativeTime();

  // Custom short relative time
  @override
  RelativeDateTime shortRelativeDateTime() => ViCustomShortRelativeTime();
}

Supported Locales #

The package supports multiple locales. Here is a list of all supported locales:

Formatting & Relative Locales Supported

  • am - Amharic
  • ar - Arabic
  • az - Azerbaijani
  • be - Belarusian
  • bn - Bengali
  • bs - Bosnian
  • ca - Catalan
  • cs - Czech
  • da - Danish
  • de - German
  • en - English
  • es - Spanish
  • et - Estonian
  • fa - Persian
  • fi - Finnish
  • fr - French
  • he - Hebrew
  • hi - Hindi
  • hr - Croatian
  • hu - Hungarian
  • id - Indonesian
  • it - Italian
  • ja - Japanese
  • ka - Georgian
  • km - Khmer
  • ko - Korean
  • lv - Latvian
  • mn - Mongolian
  • ms_my - Malay
  • my - Burmese
  • nb - Norwegian BokmΓ₯l
  • nl - Dutch
  • pl - Polish
  • ps - Pashto
  • pt - Portuguese
  • ro - Romanian
  • ru - Russian
  • sv - Swedish
  • th - Thai
  • tl_PH - Filipino
  • tr - Turkish
  • uk - Ukrainian
  • vi - Vietnamese
  • zh - Chinese

Only Supported Relative Locales

  • dv - Divehi
  • gr - Greek
  • ku - Kurdish
  • nn - Norwegian Nynorsk
  • rw - Kinyarwanda
  • tk - 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.

5
likes
160
points
385
downloads

Publisher

unverified uploader

Weekly Downloads

A Flutter package for formatting dates and times in various locales. This package provides utilities for formatting relative dates, ordinal numbers, and more.

Repository (GitHub)

Documentation

API reference

License

MIT (license)

Dependencies

intl

More

Packages that depend on flutter_date_formatter