Flutter
DateTime
Flutter Development
Mobile App Development
Programming Tips

How to format DateTime in Flutter

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

In Flutter, dealing with dates and times often requires formatting them into a readable and user-friendly format. The standard Dart library provides simple ways to achieve this using the intl package, which offers robust support for internationalization, including date and time formatting. This article provides a comprehensive guide on formatting DateTime in Flutter, covering the essentials, technical background, and practical examples.

Setting Up the Environment

Before formatting DateTime in your Flutter project, make sure you have the intl package added to your pubspec.yaml file:

yaml
1dependencies:
2  flutter:
3    sdk: flutter
4  intl: ^0.17.0

Run flutter pub get to install the package.

DateTime Basics in Dart

In Dart, dates and times are typically handled using the DateTime class. This class provides various methods and properties to work with date and time.

Example

dart
1void main() {
2  DateTime now = DateTime.now();
3  print('Current Date and Time: $now');
4}

Formatting DateTime

The intl package's DateFormat class is used extensively for formatting dates. The class provides several predefined patterns for general use and supports custom patterns as well.

Common Date Formats

Below are some common date formatting patterns:

  • yyyy-MM-dd: Year-Month-Day (e.g., 2023-10-01)
  • dd-MM-yyyy: Day-Month-Year (e.g., 01-10-2023)
  • MMMM d, y: Month day, Year (e.g., October 1, 2023)
  • EEE, MMM d, ''yy: Weekday, Month day, 'Year (e.g., Mon, Oct 1, '23)

Technical Explanation

The DateFormat class uses pattern strings to format DateTime objects.

Example

dart
1import 'package:intl/intl.dart';
2
3void main() {
4  DateTime now = DateTime.now();
5
6  // Standard Year-Month-Day format
7  String formatted = DateFormat('yyyy-MM-dd').format(now);
8  print('Formatted Date: $formatted');
9
10  // Custom format: Month day, Year
11  String customFormat = DateFormat('MMMM d, y').format(now);
12  print('Custom Formatted Date: $customFormat');
13}

Locale-Based Formatting

The DateFormat class also supports locale-based formatting. This is particularly useful for internationalized apps.

Example

dart
1import 'package:intl/intl.dart';
2
3void main() {
4  DateTime now = DateTime.now();
5  // Format for French locale
6  String frenchFormatted = DateFormat.yMMMMd('fr_FR').format(now);
7  print('Date in French Locale: $frenchFormatted');
8}

Handling Time

Time formatting can be done similarly using the DateFormat class. Common patterns include:

  • HH:mm:ss: 24-hour format with seconds
  • hh:mm a: 12-hour format with AM/PM

Example

dart
1import 'package:intl/intl.dart';
2
3void main() {
4  DateTime now = DateTime.now();
5
6  // 24-hour format
7  String time24 = DateFormat('HH:mm:ss').format(now);
8  print('24-hour format: $time24');
9
10  // 12-hour format with AM/PM
11  String time12 = DateFormat('hh:mm a').format(now);
12  print('12-hour format: $time12');
13}

Summary Table

FeatureDescriptionExample Format
Date FormattingStructures date into readable formatyyyy-MM-dd MMMM d, y
Locale-Based FormattingFormats date based on localeyMMMMd('fr_FR')
Time FormattingFormats time in 24-hour or 12-hour formatHH:mm:ss hh:mm a

Additional Topics

Parsing Date Strings

Apart from formatting, you might need to parse date strings into DateTime objects. The DateFormat class also provides functionalities for parsing.

dart
String dateString = "2023-10-01";
DateTime parsedDate = DateFormat('yyyy-MM-dd').parse(dateString);
print('Parsed Date: $parsedDate');

Handling Time Zones

Handling time zones in DateTime requires understanding Dart's DateTime.utc and DateTime.local constructors. However, the intl package is primarily focused on formatting rather than time zone conversions, which may require using additional packages like timezone.

Conclusion

Formatting DateTime in Flutter is a crucial skill, especially when developing applications that require displaying dates and times in various formats or for international audiences. Utilizing the intl package provides great flexibility and power in both formatting and parsing operations, ensuring your Flutter applications handle DateTime efficiently and elegantly.


Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.