flutter_datetime_format 0.0.4
flutter_datetime_format: ^0.0.4 copied to clipboard
A Flutter plugin for formatting DateTime objects into custom date and time strings with predefined aliases and custom formats.
Flutter Datetime Format π #
A simple plugin to format dates and times in Flutter. π #
Introduction π #
The flutter_datetime_format
package provides an easy-to-use plugin for formatting DateTime
objects into custom date/time strings. This plugin supports common date and time placeholders, which can be formatted to display full month names, abbreviated weekdays, and more. It is highly flexible, allowing developers to specify custom format patterns using predefined aliases. π
Format Aliases π€ #
You can use the following placeholders (aliases) in the custom format strings:
YYYY
: Year (e.g., 2024) πMM
: Month as two digits (e.g., 09 for September) ποΈMMMM
: Full month name (e.g., September) πΌDD
: Day of the month as two digits (e.g., 05) π’EEE
: Abbreviated weekday name (e.g., Sun, Mon) πHH
: Hour (24-hour format, e.g., 14 for 2 PM) β°hh
: Hour (12-hour format, e.g., 02 for 2 PM) πmm
: Minutes as two digits (e.g., 05) β²οΈss
: Seconds as two digits (e.g., 30) β±οΈmd
: AM/PM marker (e.g., AM, PM) π
Installation π¦ #
To use this package, add flutter_datetime_format
as a dependency in your pubspec.yaml
file.
dependencies:
flutter_datetime_format: ^0.0.1
Then, run:
flutter pub get
Example Usage π» #
Here is an example of how to use the flutter_datetime_format
to format dates and times:
import 'package:flutter_datetime_format/flutter_datetime_format.dart' as fd;
void main() {
DateTime now = DateTime.now();
// Example 1: Format as 'DD/MM/YYYY'
String formattedDate1 = FLDateTime.formatTime(now, 'DD/MM/YYYY');
print(formattedDate1); // Output: 22/09/2024
// Example 2: Format as 'YYYY-MM-DD HH:mm:ss'
String formattedDate2 = FLDateTime.formatTime(now, 'YYYY-MM-DD HH:mm:ss');
print(formattedDate2); // Output: 2024-09-22 14:05:30
// Example 3: Format with full month name and weekday
String formattedDate3 = FLDateTime.formatTime(now, 'EEE, MMMM DD, YYYY');
print(formattedDate3); // Output: Sun, September 22, 2024
// Example 4: 12-hour format with AM/PM
String formattedDate4 = FLDateTime.formatTime(now, 'hh:mm a');
print(formattedDate4); // Output: 02:05 PM
// New Example 5: Using the updated method 'formatTime' for custom format
String formattedDate5 = FLDateTime.formatTime(now, 'DD MMMM YYYY, hh:mm:ss a');
print(formattedDate5); // Output: 22 September 2024, 02:05:30 PM
// New Example 6: Using the method 'formatRelative' for relative date formatting
String relativeDate = FLDateTime.formatRelative(now.subtract(Duration(days: 1)));
print(relativeDate); // Output: 1 day ago (depends on the current time)
}
Key Updates: #
formatTime(date,format)
: AddformatTime
for localized date formatting and custom format support.formatRelative(date,ref)
: AddformatRelative
for formatting relative time like "2 hours ago" or "in 3 days".- deprecate:
formatCustom
β UseformatTime
instead. - deprecate:
formatHour12
β UseformatTime
instead. - deprecate:
formatName
β UseformatTime
instead. - fix: Handle
null
DateTime
values with fallback string informatTime
.
Resources π #
Feel free to contribute, report issues, or request new features on GitHub.
I've added the chips section at the end, which includes links to the Flutter, Dart, Bloc, and State Management docs. You can customize the chip links further according to your needs.
Let me know if you need further modifications!