Download StackFood Multi Restaurant, Food Delivery App with Laravel Admin and Restaurant Panel Buy Full App on $79
Download FlutKit - Flutter UI Kit Most beautiful Flutter UI Kit with 20+ Full App, 450+ Screens. Buy Full Kit on $39
Download Fluxstore Multi Vendor WooCommerce/Dokan Plugin compatible WordPress eCommerce Flutter App. Buy Full App on $79

How to Format Date and Time in Dart/Flutter

In this example, we are going to show you the easiest way to format date time in your desired pattern. You can format date and time in different combinations mixed with year, day, weekday, month, month name, hour, minute, second, and many more.

First, add, intl Flutter package in your project by adding the following lines in pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.17.0

How to get the current date and time:

DateTime datetime = DateTime.now();
print(datetime.toString());
//output: 2021-10-17 20:04:17.118089

How to format date and time using the intl Flutter package.

DateTime datetime = DateTime.now();
print(datetime.toString());
//output: 2021-10-17 20:04:17.118089

String datetime1 = DateFormat("yyyy-MM-dd").format(datetime);
print(datetime1);
//output: 2021-10-17

String datetime2 = DateFormat.Hms().format(datetime);
print(datetime2);
//output (Time): 20:04:17

String datetime3 = DateFormat.MMMMEEEEd().format(datetime);
print(datetime3);
//output: Sunday, October 17

DateFormat() is from intl package.

How to get Year, Month, Day, Month, Second from DateTime():

DateTime datetime = DateTime.now();
print(datetime.toString());
//output: 2021-10-17 20:04:17.118089

String year = datetime.year.toString();
print(year); //output: 2021

String month = datetime.month.toString();
print(month); //Output: 10

String hour = datetime.hour.toString();
print(hour); //output (24 hour format): 20

String minute = datetime.minute.toString();
print(minute);

String second = datetime.second.toString();
print(second);

More Date and Time Formatting options:

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

Flutter Code Example:

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

void main() {
  runApp(
    MaterialApp( 
      home: MyApp()
    )
  );
}

class MyApp extends StatefulWidget{
  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {

  @override
  Widget build(BuildContext context) {

    DateTime datetime = DateTime.now();
    print(datetime.toString());
    //output: 2021-10-17 20:04:17.118089

    String datetime1 = DateFormat("yyyy-MM-dd").format(datetime);
    print(datetime1);
    //output: 2021-10-17

    String datetime2 = DateFormat.Hms().format(datetime);
    print(datetime2);
    //output (Time): 20:04:17

    String datetime3 = DateFormat.MMMMEEEEd().format(datetime);
    print(datetime3);
    //output: Sunday, October 17


    String datetime4 = DateFormat(DateFormat.YEAR_ABBR_MONTH_WEEKDAY_DAY).format(datetime);
    print(datetime4);
    //output: Sun, Oct 17, 2021

    String year = datetime.year.toString();
    print(year); //output: 2021

    String month = datetime.month.toString();
    print(month); //Output: 10

    String hour = datetime.hour.toString();
    print(hour); //output (24 hour format): 20

    String minute = datetime.minute.toString();
    print(minute);

    String second = datetime.second.toString();
    print(second);

    return Scaffold(
          appBar: AppBar(
              title:Text("Format Date and Time"), //appbar title
              backgroundColor: Colors.redAccent //appbar background color
          ),
          body: Container(
            alignment:Alignment.topCenter,
            padding: EdgeInsets.all(35),
             child: Column( 
                children:[

                  Text(datetime1),

                  Text(datetime2),

                  Text(datetime3),

                  Text(datetime4),
                ]
             )
          )
      );
  }
}

Screenshot Output:

In this way, you can format the date and time in Dart and Flutter.

No any Comments on this Article


Please Wait...