Hello guys. How are you all. Hope you all are fine. Today in this tutorial we are going to learn about Formate Datetime in flutter. So lets start this tutorial without wasting your time.
First of all we need is Intl package. so add this in your pubspec.yaml file and then get dependency and then import in your dart file.
Get current date in flutter
You can get current date in flutter Date in any format. Just by using DateFormat() and pass formate that you want then use format function by providing now date.
var todayDate = DateFormat("dd-MM-yyyy").format(now);
print(todayDate); //30-06-2021
Get current time in flutter
To get current time in flutter just pass DateFormate(‘H:m:s’) and it will return Hour: Minutes and second formate.
var todayDate = DateFormat("H:m:s).format(now);
print(todayDate); //15:22:52
Format Date in flutter
If you want to format date to another format then this is easy. Explore below example.
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
Convert String to Datetime in flutter
You can also parse string to datetime. here is example.
String usrDate = "2021/06/30";
var todayDate = DateFormat("yyyy-MM-dd", "en_US").parse(usrDate);
print(todayDate); //30-06-2021
Parsing DateTime in flutter
You can also parse string to datetime. here is example.
String usrDate = "2021/06/30";
var todayDate = DateFormat("yyyy-MM-dd", "en_US").parse(usrDate);
print(todayDate); //30-06-2021
Compare two date in flutter
You can also Compare two date in flutter. Here is example.
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
Different between two Dates in flutter
You can also get Different between two Dates in flutter. Here is example.
var dayDifferent=date2.difference(date1);
print(dayDifferent.inDays); //125
Add days into the Date in flutter
You can also Add days into the Date in flutter. Here is example.
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
Faq
- How to Format DateTime in Flutter?
You can get the current date in flutter Date in any format. Just by using DateFormat() and pass formate that you want then use format function by providing now date. To get the current time in flutter just pass DateFormate('H:m:s') and it will return Hour: Minutes and second format. If you want to format the date to another format then this is easy. Explore the below example.
- Format DateTime in Flutter?
You can get the current date in flutter Date in any format. Just by using DateFormat() and pass formate that you want then use format function by providing now date. To get the current time in flutter just pass DateFormate('H:m:s') and it will return Hour: Minutes and second format. If you want to format the date to another format then this is easy. Explore the below example.
Summery
So, It’s All About Format DateTime in Flutter. I hope this tutorial helps you to solve your error. Please Comment Below if You stucks anywhere with my code.
Also Check Out Below Tutorials
- No named parameter with the name ‘nullOk’ error in flutter
- Gradle failure may have been because of AndroidX incompatibilities in this Flutter app
- Flutter: CocoaPods’s specs repository is too out-of-date to satisfy dependencies
- The argument type ‘Future’ can’t be assigned to the parameter type ‘void Function()’
- Flutter SDK is not found in the specified location in Android Studio
Leave a Reply