How to change edit date format (Date picker) in flutter

Viewed 7021

In the current date picker ui, the date format is MM/DD/YYYY. I want to change to DD/MM/YYYY. Please guide.

showDatePicker(
initialDatePickerMode: DatePickerMode.day,
helpText: ' ',
currentDate: DateTime.now(),
context: context,
initialDate: selectedDate,
firstDate: DateTime(1960, 1, 1),
lastDate: DateTime(2200, 12, 31),
builder: (BuildContext context, Widget child) {
  return Theme(
    data: ThemeData(
        primarySwatch: createMaterialColor(const Color(0xFF1CA357))),
    child: child,
  );
},

);

date picker ui

2 Answers

Try to use this (eg. for de locale):

Add internationalization processing and support in pubspec.yaml:

dependencies:
  flutter:
    sdk: flutter
  flutter_localizations:
    sdk: flutter   

Add internationalization support in the top-level control MaterialApp:

MaterialApp(
  localizationsDelegates: [
    GlobalMaterialLocalizations.delegate,
    GlobalWidgetsLocalizations.delegate,
  ],
  supportedLocales: [
    Locale('de', 'DE'),
    Locale('en', 'US'),
  ],
  locale: Locale('de'),
  ...
)

Then set the `locale parameter of showDatePicker as follow:

final date = await showDatePicker(locale: Locale('de'));

Add Puspec.yaml:

dependencies:
    flutter:
        sdk: flutter
    flutter_localizations:
        sdk: flutter

Add the following code in main.dart:

import 'package:flutter_localizations/flutter_localizations.dart';    
        
void main() async {
    runApp(MaterialApp(
        //home: SplashScreen(),
        // List all of the app's supported locales here
        supportedLocales: [
            Locale( 'in' , 'IN' ),
            Locale( 'en' , 'US' ),
            Locale( 'de' , 'DE' ),
        ],
            
        localizationsDelegates: [
            GlobalMaterialLocalizations.delegate,
            GlobalWidgetsLocalizations.delegate,
        ],
            
        localeResolutionCallback: (locale, supportedLocales) {
            for (var supportedLocaleLanguage in supportedLocales) {
                if (supportedLocaleLanguage.languageCode == locale.languageCode && supportedLocaleLanguage.countryCode == locale.countryCode) {
                    return supportedLocaleLanguage;
                }
            }
            return supportedLocales.first;
        },
        locale: Locale('in'),
        home: Home(),
        debugShowCheckedModeBanner: false,
        theme: ThemeData(
            brightness: Brightness.light,
            primaryColor: AppColor.primaryColor,
            accentColor: AppColor.accentColor,
        )
    );
}
Related