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.
Introduction
In Flutter, format DateTime objects using the intl package's DateFormat class. Dart's built-in DateTime has no formatting method beyond .toString() and .toIso8601String(). The intl package provides DateFormat with pattern-based formatting (like yyyy-MM-dd) and locale-aware output (month names, day names in different languages). For simple cases without adding a dependency, you can also build format strings manually from DateTime properties.
Setup: Add the intl Package
Basic Formatting with DateFormat
Pattern Reference
| Pattern | Meaning | Example |
yyyy | 4-digit year | 2025 |
yy | 2-digit year | 25 |
MM | 2-digit month | 09 |
M | Month without padding | 9 |
MMM | Abbreviated month name | Sep |
MMMM | Full month name | September |
dd | 2-digit day | 23 |
d | Day without padding | 3 |
EEEE | Full weekday name | Tuesday |
EEE | Abbreviated weekday | Tue |
HH | 24-hour hour (00-23) | 14 |
hh | 12-hour hour (01-12) | 02 |
mm | Minutes | 30 |
ss | Seconds | 45 |
a | AM/PM | PM |
S | Fractional seconds | 3 |
Named Constructors (Predefined Formats)
Named constructors are locale-aware and adjust output based on the user's locale.
Locale-Aware Formatting
Call initializeDateFormatting before using locale-specific formats. Pass the locale string as the first argument to DateFormat or its named constructors.
Manual Formatting (Without intl)
Manual formatting avoids the intl dependency but does not support locale-aware month/day names.
Parsing Strings to DateTime
Relative Time (Time Ago)
Flutter Widget Example
Common Pitfalls
- Month vs minute confusion:
MMis month,mmis minutes.DateFormat('yyyy-mm-dd')formats minutes in the month position. Always use uppercaseMMfor months. - Missing locale initialization: Using
DateFormat.yMMMMd('fr_FR')without callinginitializeDateFormatting('fr_FR')first throws aLocaleDataException. Initialize locales at app startup. - 24-hour vs 12-hour format:
HHis 24-hour (00-23),hhis 12-hour (01-12). Usinghhwithouta(AM/PM) makes afternoon times ambiguous. - UTC vs local time:
DateTime.now()returns local time.DateTime.now().toUtc()returns UTC. Formatting does not convert between zones — it formats whateverDateTimeit receives. Always be explicit about which zone yourDateTimeis in. - Parsing strict vs lenient:
DateFormat.parse()is lenient by default.DateFormat('yyyy-MM-dd').parse('2025-13-45')may not throw. UseparseStrict()for validation:DateFormat('yyyy-MM-dd').parseStrict('2025-13-45')throws aFormatException.
Summary
- Use the
intlpackage'sDateFormatfor flexible DateTime formatting in Flutter - Pattern-based:
DateFormat('yyyy-MM-dd HH:mm')for custom formats - Named constructors:
DateFormat.yMd(),DateFormat.jm()for locale-aware defaults - Initialize locale data with
initializeDateFormatting()before using non-English locales - Use
DateTime.parse()orDateFormat.parse()to convert strings back to DateTime - For simple formats without dependencies, build strings manually from DateTime properties
Related reading
- How to generate a random number in Swift?
- How to generate buildConfigField with String type
- How to generate UUID in ios
- How to get a context in a recycler view adapter
- How to get a context in a recycler view adapter
- How to get a Fragment to remove itself, i.e. its equivalent of finish?
- How to Get a Layout Inflater Given a Context?
- How to get a list of installed android applications and pick one to run
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.