Working With DateTime in Flutter

In this tutorial we are going to learn how to format DateTime in Flutter. So, without further ado, let’s get started with this tutorial. First and foremost, we require the Intl package. Add this to your pubspec.yaml file, then get the dependency and import it into your dart program.

How To Get Current Date In Flutter?

In flutter Date, you may get the current date in any format. Simply call DateFormat() and supply the formate you want, then apply the format function with the now-provided date.

var todayDate = DateFormat("dd-MM-yyyy").format(now); print(todayDate); //30-06-2021
Code language: PHP (php)

How To Get Current Time In Flutter?

Simply pass DateFormate(‘H:m:s’) to get the current time in flutter, and it will return Hour: Minutes and Second formate.

var todayDate = DateFormat("H:m:s).format(now); print(todayDate); //15:22:52
Code language: PHP (php)

How To Format Date In Flutter?

This is simple to do if you wish to format a date in a different format. Consider the case below.

var todayDate = DateFormat("dd-MM-yyyy").format(now); print(todayDate); //30-06-2021 var todayDate = DateFormat("yyyy-MM-dd hh:mm:ss").format(now); print(todayDate); //2021-06-30 05:22:52 var todayDate = DateFormat("dd-MM-yyyy hh:mm:ss").format(now); print(todayDate); //30-06-2021 05:22:52
Code language: PHP (php)

How To Convert String To Datetime In Flutter?

You can also convert a string to a datetime format. Here’s an illustration.

String usrDate = "2021/06/30"; var todayDate = DateFormat("yyyy-MM-dd", "en_US").parse(usrDate); print(todayDate); //30-06-2021
Code language: JavaScript (javascript)

How To Parse DateTime In Flutter?

It’s also possible to convert a string to a datetime. As an illustration, consider the following:

String usrDate = "2021/06/30"; var todayDate = DateFormat("yyyy-MM-dd", "en_US").parse(usrDate); print(todayDate); //30-06-2021
Code language: JavaScript (javascript)

How To Compare Two Date In Flutter Using DateTime?

In flutter, you may also compare two dates. As an example, consider the following.

var pastDate = DateTime.parse("2000-05-10 22:19:23"); var futureDate = DateTime.parse("2025-05-10 22:19:23"); print(date1.isBefore(date2)); // => true print(date1.isAfter(date2)); // => false
Code language: PHP (php)

How To Differentiate Between Two Dates In Flutter?

In flutter, you can also get the difference between two dates. As an example, consider the following.

var dayDifferent=date2.difference(date1); print(dayDifferent.inDays); //125
Code language: PHP (php)

How To Add Days Into A Date In Flutter?

In flutter, you may also add days to the Date. As an example, consider the following.

var date1 = DateTime.parse("2021-06-30 20:18:04"); var newDate = date1.add(new Duration(days: 2)); print(newDate); // => 2021-07-02 20:18:04
Code language: PHP (php)

FAQ

  1. How to format DateTime in Flutter: To format a DateTime in Flutter, follow these steps. In flutter Date, you may get the current date in any format. Simply call DateFormat() and supply the formate you want, then apply the format function with the now-provided date. Simply pass DateFormate(‘H:m:s’) to get the current time in flutter, and it will return Hour: Minutes and Second format. This is simple to do if you wish to format the date in a different way. Look at the example below.
  2. Format DateTime in Flutter: In Flutter, how to format DateTime The current date can be obtained in any format with flutter Date. Simply call DateFormat() and supply the desired formate, then apply the format function with the provided date. Simply pass DateFormate(‘H:m:s’) in flutter to get the current time in Hour: Minutes and Second format. It’s simple to change the date format. Consider the illustration below.

Conclusion

I hope this article helped you understand how to work with Dates in Flutter. As always, If you have found this article useful do not forget to share it and leave a comment if you have any questions. Happy Coding 🙂

%d bloggers like this: