How to format string only Time and compare it with the current Time.
var openTime = '08:00';
var closeTime = '20:00';
var now = DateTime.now();
var formatterTime = DateFormat('kk:mm');
var time = formatterTime.format(now);
Below line Thrown invalid format
var actualTime = DateTime.parse(time);
var closeTime = DateTime.parse(openTime);
var openTime = DateTime.parse(closeTime);
if (closeTime.isBefore(actualTime)) {
isOpened == false;
}
The DateTime.parse function only accepts certain formats as an input. If you want to compare the current time to the closing time, I recommend creating the closing time as a DateTime then compare it as follows
DateTime eightAmToday = DateTime(
DateTime.now().year, DateTime.now().month, DateTime.now().day, 8, 0, 0);
DateTime eightPmToday = DateTime(DateTime.now().year, DateTime.now().month,
DateTime.now().day, 20, 0, 0);
DateTime now = DateTime.now();
if(eightPmToday.isBefore(now)) {
log.i('Closed');
} else {
log.i('Open');
}
Then if you want to display the time as a string in the format 08:00, for example, you can use the following
DateFormat formattedTime = DateFormat('kk:mm');
String displayTime = formattedTime.format(now);
If you are looking for a different way to display your DateTime as a string I recommend looking through the list of accepted strings in the documentation https://api.flutter.dev/flutter/intl/DateFormat-class.html