Reputation: 373
I'm working on a problem which requires to compare the date with other if the selected date is less than given then print Hello.
The date is present In String Format like given in Examaple Ex:
if('2020-1-13'<'2021-1-5')
{
print(hello);
}
Upvotes: 0
Views: 289
Reputation: 90025
package:basics
provides DateTime
extensions that can easily compare DateTime
objects.
(Disclosure: I contributed those particular extensions.)
Upvotes: 0
Reputation: 98
You should try this it should work
````
var date1 = DateTime.parse('2020-01-13').millisecondsSinceEpoch.toInt();
var date2 = DateTime.parse('2021-01-15').millisecondsSinceEpoch.toInt();
if(date1 < date2){
print('true');
}else{
print('false');
}
````
Upvotes: 3
Reputation: 6875
Instead of doing that just use the date's isBefore
or isAfter
operator
It would look something like the following:
final now = DateTime.now();
final yesterday = DateTime.now().subtract(Duration(days: 1));
print(now.isAfter(yesterday)); //true
Since your dates are in STring format, it you would have to use the DateTime.parse() method to construct your date objects, and then use the operators talked about above
Upvotes: 2