DEV Community

Matheus Paixão
Matheus Paixão

Posted on

Flutter - How to format DateTime and how to display it

Hello,
Today I will show you how to format datetime in the way you wish to display wi the intl flutter package.

To check the latest version o the package check this link HERE

Installation and Importing

At pubspec.yaml add intl: ^0.17.0 and make sure you check for the latest version, the ^ sign before the version is for the system to auto-update within that main release. So when they release version 0.17.1 it auto-update it self.

dependencies:
  flutter:
    sdk: flutter
  intl: ^0.17.0
Enter fullscreen mode Exit fullscreen mode

At the file you wish to use, import it like this.

import 'package:intl/intl.dart';
Enter fullscreen mode Exit fullscreen mode

How to use

Variable - datetime

You can edit and format as you wish, with dots, comma, arrows, etc.

DateFormat('d MMM y').format(datetime) = 28 Apr 2022
DateFormat('d MM y').format(datetime) = 28 04 2022
DateFormat('d-MM-y').format(datetime) = 28-04-2022

There are also a list o functions that we can use that gives us default formats.

DateFormat.yQQQQ().format(datetime) = 2nd quarter 2022

Have a look at all formats available at the official documentation HERE

Thank you

Top comments (0)