How to Compare Two DateTime in Dart/Flutter
i-want-to-make-sure-that-users-can-put-numbers-only-from-1-10-in-textformfield
datetime-comparison-in-dart
how-to-compare-two-datetime-in-dart-flutter
compare+two+dates+dart
compare+a+date+and+time+with+current+date+in+flutter
dart-Datetime-compare-dates
Flutter get Weekend and Month
How to Get Current Date and Time on Flutter
How to Get Current Date and Time on Flutter
Get Last Month Date In Flutter / Dart
How get the name of the days of the week in Dart
Question
How to Compare Two DateTime in Dart/Flutter
How to add and subtract days from current date
Flutter get Weekend and Month
How to Get Current Date and Time on Default Format:
How to Get Year/Month/Day and Hour/Minute/Second/
How to Get Date in Formatted String
How to get Time on Formatted String
How to get Time on 12-Hour Formatted String with AM and PM:
Get Last Month Date In Flutter / Dart
How get the name of the days of the week in Dart
How to Compare Two DateTime in Dart/Flutter
How to Compare Two DateTime in Dart/Flutter
How to Compare Two DateTime in Dart/Flutter
DateTime.compareTo(DateTime other):
DateTime dt1 = DateTime.parse("2021-12-23 11:47:00");
DateTime dt2 = DateTime.parse("2018-02-27 10:09:00");
if(dt1.compareTo(dt2) == 0){
print("Both date time are at same moment.");
}
if(dt1.compareTo(dt2) < 0){
print("DT1 is before DT2");
}
if(dt1.compareTo(dt2) > 0){
print("DT1 is after DT2");
}
DateTime.isAtSameMomentAs(DateTime other):
DateTime dt1 = DateTime.parse("2021-12-23 11:47:00");
DateTime dt2 = DateTime.parse("2018-02-27 10:09:00");
if(dt1.isAtSameMomentAs(dt2)){
print("Both DateTime are at same moment.");
}
DateTime.isBefore(DateTime other):
DateTime dt1 = DateTime.parse("2021-12-23 11:47:00");
DateTime dt2 = DateTime.parse("2018-02-27 10:09:00");
if(dt1.isBefore(dt2)){
print("DT1 is before DT2");
}
DateTime.isAfter(DateTime other):
DateTime dt1 = DateTime.parse("2021-12-23 11:47:00");
DateTime dt2 = DateTime.parse("2018-02-27 10:09:00");
if(dt1.isAfter(dt2)){
print("DT1 is after DT2");
}
** Flutter/Dart Full Example**:
import 'package:flutter/material.dart';
void main() {
runApp( MaterialApp(
home: Home()
));
}
class Home extends StatefulWidget {
@override
State<Home> createState() => _HomeState();
}
class _HomeState extends State<Home> {
@override
Widget build(BuildContext context) {
DateTime dt1 = DateTime.parse("2021-12-23 11:47:00");
DateTime dt2 = DateTime.parse("2018-02-27 10:09:00");
return Scaffold(
appBar: AppBar(
title: Text("Compare Date Time in FLutter"),
backgroundColor: Colors.redAccent,
),
body: Container(
alignment: Alignment.center,
padding: EdgeInsets.all(20),
child: Column(
children:[
Text("DT 1:" + dt1.toString()),
Text("DT 2" + dt2.toString()),
Text(dt1.isAfter(dt2)?"DT1 is after DT2":"DT1 is not after DT2"),
Text(dt1.isBefore(dt2)?"DT1 is before DT2":"DT1 is not before DT2"),
]
),
)
);
}
}
How to Comparing only dates of DateTimes in Dart or Flutter Programming| Dart by Example
How to Compare only dates of DateTimes in Dart?
Create three Date time objects,
- First object date1 is using now() - 2022-04-10 20:48:56.354
- Second object date2 created by adding duration of 10 seconds - 2022-04-10 20:49:06.354
- Third object date3 created by adding duration of 10 days - 2022-04-20 20:48:56.354
- date1 and date2 are equal, and date1,and date3 or date2 and date3 are not equal
There are multiple ways we can check only the Dates of a DateTime object.
void main() {
DateTime date1 = DateTime.now();
print(date1); //2022-04-10 20:48:56.354
var date2 = date1.add(Duration(seconds: 10));
print(date2); //2022-04-10 20:49:06.354
var date3 = date1.add(Duration(days: 10));
print(date3); //2022-04-20 20:48:56.354
print(date1.isDateEqual(date2)); // true
print(date1.isDateEqual(date3)); // false
print(date1.isDateEqual(date3)); // false
}
extension CompareDates on DateTime {
bool isDateEqual(DateTime date2) {
return year == date2.year && month == date2.month && day == date2.day;
}
}
Output
2022-04-10 20:48:56.354
2022-04-10 20:49:06.354
2022-04-20 20:48:56.354
true
false
false
use the difference and inDays method
Duration difference(DateTime other)
It returns the Duration object, calls inDays that returns a number of days, checks days are zero or not.
Here is an example program.
void main() {
DateTime date1 = DateTime.now();
print(date1); //2022-04-10 20:48:56.354
var date2 = date1.add(Duration(seconds: 10));
print(date2); //2022-04-10 20:49:06.354
var date3 = date1.add(Duration(days: 10));
print(date3); //2022-04-20 20:48:56.354
print(date1.difference(date2).inDays == 0); // true
print(date1.difference(date3).inDays == 0); // false
print(date2.difference(date3).inDays == 0); // false
}
Output
2022-04-10 20:48:56.354
2022-04-10 20:49:06.354
2022-04-20 20:48:56.354
true
false
false
How to Get Current Date and Time on Flutter
In this example, we are going to show you the easiest way to get the current date and time on the Flutter app with the dart. We will also show you to get date and time with different kinds of formats as well. See below for more details.
How to Get Current Date and Time on Default Format:
String datetime = DateTime.now().toString();
print(datetime);
// 2021-08-27 19:14:57.142575
How to Get Year/Month/Day and Hour/Minute/Second/
var dt = DateTime.now();
print(dt.year);
// -> Year | Output: 2021
print(dt.month);
// -> Month | Output: 8
print(dt.day);
// -> Day | Output: 27
print(dt.hour);
// -> Hour | Output: 19
print(dt.minute);
// -> Minute | Output: 19
print(dt.second);
// -> Second | Output: 2
How to Get Date in Formatted String
To get date and time on a formatted string, first, you need to add intl flutter package on your project dependency by adding the following lines on pubspec.yaml file.
dependencies:
flutter:
sdk: flutter
intl: ^0.17.0
import 'package:intl/intl.dart';
Now to get date on a formatted string, follow the steps below:
String cdate = DateFormat("yyyy-MM-dd").format(DateTime.now());
print(cdate);
output: 2021-08-27
String cdate1 = DateFormat("EEEEE, dd, yyyy").format(DateTime.now());
print(cdate1);
output: Friday, 27, 2021
String cdate2 = DateFormat("MMMM, dd, yyyy").format(DateTime.now());
print(cdate2);
output: August, 27, 2021
String cdate3 = DateFormat("MMM, EEE, yyyy").format(DateTime.now());
print(cdate3);
output: Aug, Fri, 2021
How to get Time on Formatted String
String tdata = DateFormat("HH:mm:ss").format(DateTime.now());
print(tdata);
output: 19:36:01
How to get Time on 12-Hour Formatted String with AM and PM
String tdata = DateFormat("hh:mm:ss a").format(DateTime.now());
print(tdata);
output: 07:38:57 PM
Get Last Month Date In Flutter / Dart
ou can just use
var prevMonth = new DateTime(date.year, date.month - 1, date.day);
with
var date = new DateTime(2018, 1, 13);
you get
2017-12-13
How get the name of the days of the week in Dart
Use 'EEEE' as a date pattern
DateFormat('EEEE').format(date);
Thursday
Latest comments (0)