Date Format In Flutter Dart
how to format date time in flutter dart

Hi Guys, Welcome to Proto Coders Point, In this flutter dart article let’s learn how to format a dateTime in dart language.

For Example: Support I have a Instance of DateTime, & want to format the dateTime as per my need, let’s say I want to convert date into string using DateFormat like “yyyy-MM-dd” – > 2022-06-02.

To Acheive DateFormat in dart we can make use of intl package of dart/flutter.

About intl package in dart/flutter

This flutter intl package library is very much useful when you are working on adding features into you flutter app such as internationalization, localization(including message translation), Number & Date formating.

In this article, will look into DateTime Formating.

1. How to install intl library in dart/flutter

In your project Terminal run below command to install intl package as external library.

In Dart Project:

dart pub add intl

In Flutter Project:

flutter pub add intl

So the below command will add intl as dependencies under pubspec.yaml file as external library usage.

2. Now Import intl.dart

After successfully adding intl, To use it you now need to just import it where required.

import 'package:intl/intl.dart';

How to Format Date Time in flutter dart – Examples Below

Example 1:

import 'package:intl/intl.dart';

void main(){
   final DateTime  now = DateTime.now();

  final DateFormat df = DateFormat('yyyy-MM-dd');

  final String formattedDateTime = df.format(now);

  print(formattedDateTime);    // return current date in yyyy-MM-dd Date format 2022-06-03

}

Example 2: Date Format with time

import 'package:intl/intl.dart';

void main(){
   final DateTime  now = DateTime.now();

  final DateFormat df = DateFormat('yyyy-MM-dd hh:mm');
  // If you want hours and minutes in 24 hours format use this HH:MM

  final String formattedDateTime = df.format(now);

  print(formattedDateTime);    // return current date in yyyy-MM-dd hh:mm Eg: 2022-06-03 12:24

}

There are may method been provided in intl package to format your dateTime: Below is from DateFormat offical Document.

DateFormat method to format date Time month year etc.

ICU Name                   Skeleton
--------                   --------
DAY                          d
ABBR_WEEKDAY                 E
WEEKDAY                      EEEE
ABBR_STANDALONE_MONTH        LLL
STANDALONE_MONTH             LLLL
NUM_MONTH                    M
NUM_MONTH_DAY                Md
NUM_MONTH_WEEKDAY_DAY        MEd
ABBR_MONTH                   MMM
ABBR_MONTH_DAY               MMMd
ABBR_MONTH_WEEKDAY_DAY       MMMEd
MONTH                        MMMM
MONTH_DAY                    MMMMd
MONTH_WEEKDAY_DAY            MMMMEEEEd
ABBR_QUARTER                 QQQ
QUARTER                      QQQQ
YEAR                         y
YEAR_NUM_MONTH               yM
YEAR_NUM_MONTH_DAY           yMd
YEAR_NUM_MONTH_WEEKDAY_DAY   yMEd
YEAR_ABBR_MONTH              yMMM
YEAR_ABBR_MONTH_DAY          yMMMd
YEAR_ABBR_MONTH_WEEKDAY_DAY  yMMMEd
YEAR_MONTH                   yMMMM
YEAR_MONTH_DAY               yMMMMd
YEAR_MONTH_WEEKDAY_DAY       yMMMMEEEEd
YEAR_ABBR_QUARTER            yQQQ
YEAR_QUARTER                 yQQQQ
HOUR24                       H
HOUR24_MINUTE                Hm
HOUR24_MINUTE_SECOND         Hms
HOUR                         j
HOUR_MINUTE                  jm
HOUR_MINUTE_SECOND           jms
HOUR_MINUTE_GENERIC_TZ       jmv
HOUR_MINUTE_TZ               jmz
HOUR_GENERIC_TZ              jv
HOUR_TZ                      jz
MINUTE                       m
MINUTE_SECOND                ms
SECOND                       s

Reference Code:

import 'package:intl/intl.dart';

void main(){
   final DateTime  now = DateTime.now();

  final DateFormat df = DateFormat.yMd();   // replace this method as per your need to format date

  final String formattedDateTime = df.format(now);    //use the formater pattern and pass date in it.

  print(formattedDateTime);    //result

}

DateFormat usage : Example

MethodResult
DateFormat.yMd();6/3/2022
DateFormat.yMMM();Jun 2022
DateFormat.yMEd();Fri, 6/3/2022
DateFormat.yMMMMEEEEd();Friday, June 3, 2022
DateFormat.EEEE();Friday
DateFormat.E();Fri
DateFormat.Hm();01:39
DateFormat.Hms();01:39:26
DateFormat.d();3 // today date
DateFormat.M();6 // current running month
DateFormat.j();1 AM
DateFormat.jm();01:42 AM
DateFormat.jms();01:43:11 AM
Dart dateformat method

and much more method as listed above.