How to Load All Dart DateFormat Locale In Flutter?

· 3 min read
How to Load All Dart DateFormat Locale In flutter
How to Load All Dart DateFormat Locale In flutter

Earlier we have been through How to format DateTime In Flutter So this time we are going to How to Load All Dart DateFormat Locale In Flutter?

So Are you ready for the same  ??

Let’s dive into it.

How to Load All Dart DateFormat Locale In flutter?

We need to import a package like the below:

import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

In it’s initState() Method initializeDateFormatting. Code Snippet will look like the below:

@override
void initState() {
  super.initState();
  initializeDateFormatting();
}

Users can also take look at below code snippet.

import 'package:flutter/material.dart';
import 'package:intl/intl.dart';
import 'package:intl/date_symbol_data_local.dart';

void main() => runApp(new MyApp());

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
      title: 'Intl Demo',
      theme: new ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: new MyHomePage(title: 'Intl Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

  @override
  _MyHomePageState createState() => new _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  DateFormat dateFormat;
  DateFormat timeFormat;

  @override
  void initState() {
    super.initState();
    initializeDateFormatting();
    dateFormat = new DateFormat.yMMMMd('cs');
    timeFormat = new DateFormat.Hms('cs');
  }

  void _refresh() {
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    var dateTime = new DateTime.now();
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Center(
        child: new Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            new Text(dateFormat.format(dateTime)),
            new Text(timeFormat.format(dateTime)),
          ],
        ),
      ),
      floatingActionButton: new FloatingActionButton(
        onPressed: _refresh,
        tooltip: 'Refresh',
        child: new Icon(Icons.refresh),
      ),
    );
  }
}

The above code will give us output like the below:

DateFormat In Locale Flutter
DateFormat In Locale FlutterDateFormate In Locale Flutter

The list of all available locales in Flutter can be taken from top-level property availableLocalesForDateFormatting. Users can also use the current languageCode when initializing the DateFormat.

String languageCode = Localizations.localeOf(context).languageCode;
String dateTime = DateFormat( "dd.MM.yy HH:mm", languageCode).format(this.date);

Conclusion:

Thanks for being with us on a Flutter Journey !!!
Still, need support for Flutter Development? Drop us your valuable feedback/suggestions for the same.

FlutterAgency.com is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

FlutterAgency.com is one of the most popular online portal dedicated to Flutter Technology and daily thousands of unique visitors come to this portal to enhance their knowledge on Flutter.

Leave a Reply