Is there any way to compare two times in dart, suppose we have: 13:45 and 15:34, is there any way to find out that 15:34 is after 13:45.
I was looking at this question, and there are functions to check isBefore or isAfter, but is it possible to parse time without having to parse year, month and day?
CodePudding user response:
You can convert them to datetime and compare. ie:
void main() {
var s1 = '13:45';
var s2 = '15:34';
var t1 = DateTime.parse('2000-01-01 ${s1}');
var t2 = DateTime.parse('2000-01-01 ${s2}');
print(t1.isBefore(t2));
}
EDIT: For a more direct, string sort like comparison (which I wouldn't suggest at all for Date\time values, unless you are 100% sure they are in sortable format) you could use compareTo(). ie:
print(s1.compareTo(s2) < 0);
