system_date_time_format 0.5.0 copy "system_date_time_format: ^0.5.0" to clipboard
system_date_time_format: ^0.5.0 copied to clipboard

A plugin for getting date and time format patterns from device system settings.

SystemDateTimeFormat #

A plugin for getting date & time format from device system settings.

Pub Package Build Status Code Coverage MIT License Android support ✓ iOS support ✓ macOS support ✓ windows support ✓ linux support ✓ web support ✓


Why system_date_time_format? #

Flutter does not support retrieving date and time format patterns based on the user's system settings out of the box. However, you can use the system_date_time_format plugin to get date and time format patterns for consistent formatting in your Flutter app.

Examples #

iOS (Region: United States 🇺🇸) Result
ios_setup ios_result
Android (Region: United Kingdom 🇬🇧) Result
android_setup android_result
macOS (Region: Poland 🇵🇱) Result
macos_setup macos_result
windows (Region: United States 🇺🇸) Result
windows_setup windows_result
linux (Region: United States 🇺🇸) Result
linux_setup linux_result
web (Region: Poland 🇵🇱)
web_result

Usage #

Import import 'package:system_date_time_format/system_date_time_format.dart';,
initialize SystemDateTimeFormat and use getters to get date & time formats from device system.

Example:

import 'package:system_date_time_format/system_date_time_format.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await SystemDateTimeFormat().initialize();
  runApp(const App());
}

Note

Don't forget to call initialize() before accessing any getters:

dateFormat, mediumDateFormat, longDateFormat, timeFormat

otherwise it will throw NotInitializedError.

class App extends StatelessWidget {
  const App({super.key});

  @override
  Widget build(BuildContext context) {
    // SystemDateTimeFormat() is a Singleton
    final dateFormat = SystemDateTimeFormat().dateFormat;
    final timeFormat = SystemDateTimeFormat().timeFormat;

    print(dateFormat); // e.g. "M/d/yy"
    print(timeFormat); // e.g. "HH:mm"

    return const MaterialApp(
      home: Scaffold(),
    );
  }
}

Web #

In order to use this plugin on web app you need to add system_date_time_format.js script to your index.html:

<src="https://cdn.jsdelivr.net/gh/Nikoro/system_date_time_format@main/web/system_date_time_format.min.js"></script>

index.html

<!DOCTYPE html>
<html>
<head>
    <!--...-->
    <src="https://cdn.jsdelivr.net/gh/Nikoro/system_date_time_format@main/web/system_date_time_format.min.js"></script>
</head>
<body>
<!--...-->
</body>
</html>

Fallbacks #

In case of some error, e.g. PlatformException plugin will return fallback values .
You can setup your own values by passing them in the initialize():

SystemDateTimeFormat().initialize(
  dateFormatFallback: // default: 'M/d/yy'
  mediumDateFormatFallback: // default: 'MMM d,y'
  longDateFormatFallback: // default: 'MMMM d,y'
  timeFormatFallback: // default: 'h:mm a'
);

Testing #

As the plugin class is not static, it is possible to mock and verify its behaviour when writing tests as part of your application.
Check the source code of example_with_tests which is a modification of basic example
with dependency injection using get_it and mocks thanks to mocktail.

14
likes
0
pub points
91%
popularity

Publisher

verified publisherdominikkrajcer.com

A plugin for getting date and time format patterns from device system settings.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, flutter_web_plugins, js, plugin_platform_interface

More

Packages that depend on system_date_time_format