![datetime-formatter-in-flutter](https://mobikul.com/wp-content/uploads/2023/10/datetime-formatter-in-flutter.png)
Handling dates and times can be a tricky task. Flutter provides a DateTime formatting mechanism that makes it easier to display dates and times in a user-friendly manner. In this blog post, we’ll explore the DateTime Formatter in Flutter and learn how to format dates and times according to your app’s requirements.
Why DateTime Formatter in Flutter is Needed?
DateTime Formatting in Flutter is essential in mobile app development for several reasons. Some are listed below.
- Good User Experience: Properly formatted dates and times enhance the user experience by making it more understandable.
- Localization: Applications are usually used worldwide, and different regions have distinct date and time formatting conventions. DateTime formatting allows you to adapt to those date-time differences across the globe.
You may also check our Flutter app development page.
Implementation
Flutter offers a DateTime class that represents a point in time, and it comes with a built-in method for formatting dates and times. This method is part of the “intl” package. Please check here.
You can use the “intl” package as below
1: Add the “intl” package.
1 2 3 4 5 |
dependencies: flutter: sdk: flutter intl: ^0.17.0 |
2: Import the package into your Dart file
1 |
import 'package:intl/intl.dart'; |
3: Get the date that you want to format using the below piece of code.
1 |
DateTime now = DateTime.now(); |
4: We created a DateTime object in the above code snippet, representing the current date and time. After that, we use the DateFormat class to format it as a string in the ‘yyyy-MM-dd HH:mm:ss’ format.
1 2 3 |
String formattedDate = DateFormat('yyyy-MM-dd HH:mm:ss').format(now); print(formattedDate); // 2023-09-29 14:30:00 |
You have now successfully formatted the current date and time in the provided format.
However, Custom DateTime Formatting can also be used which is explained below.
Custom DateTime Formatting
Above all, you can customize the output according to your needs.
You can define your own date and time formats using various symbols, such as ‘yyyy’ for the year, ‘MM’ for the month, and ‘dd’ for the day. You can also use symbols like ‘E’ for the day of the week and ‘H’ for the hour.
For example, let’s format a date with a custom format:
1 2 3 |
DateTime date = DateTime(2023, 9, 29); String customFormattedDate = DateFormat('EEEE, MMMM d, y').format(date); print(customFormattedDate); Thursday, September 29, 2023 |
In addition, when working with DateTime, it’s necessary to consider time zones. You can set the timezone explicitly when formatting DateTime objects:
1 2 3 |
DateTime utcTime = DateTime.now().toUtc(); String formattedUtcTime = DateFormat('yyyy-MM-dd HH:mm:ss').format(utcTime); print(formattedUtcTime); // 2023-09-29 14:30:00 |
Conclusion
DateTime formatting is a crucial aspect of mobile app development, as it allows you to present date and time information in a user-friendly and culturally appropriate way.
To know how to use the Date Picker please check here.
Please check my other blogs here.