2

I need help to compare 2 strings that i have and i want to check if one of them has less value than the other so i can throw an alert message. My code is as followed

if (dateEntered <= date) {
  print("DateEntered wrong $dateEntered");
}

The error is that

The operator '<=' isn't defined for the class 'String'. Try defining the operator '<='

Thank you for your time

André Sousa
  • 187
  • 4
  • 17

2 Answers2

1

In order to compare dates, you need to do something like this,

    var df1 = DateFormat('dd-MM-yyyy').parse('22-10-2019');
    var df2 = DateFormat('dd-MM-yyyy').parse('25-10-2019');
    print(df1.isBefore(df2).toString());

Output: true


Edit : you will have to use install intl package inside pubspec.yaml to use code above,

dependencies:
  intl: ^0.16.0
Ravinder Kumar
  • 7,407
  • 3
  • 28
  • 54
0

Since you're comparing dates you can use the DateTime class to generate new dates and then use the helper method isBefore to do the comparison. You can also use other helper methods like isAfter and difference

 var now = new DateTime.now();
 var earlier = now.subtract(const Duration(seconds: 5));

  if(earlier.isBefore(now)){
    print('Earlier is 5 seconds before now, so earlier.isBefore(now) is true');
  }
SRR
  • 1,608
  • 1
  • 21
  • 51