Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: Check if date is between two dates

Tags:

flutter

dart

I need to check date is between two dates or not.

I tried to search it but didn't got fruitful results. May be you have seen such scenarios. So, seeking your advise.

Here is my code.

var service_start_date = '2020-10-17';
var service_end_date = '2020-10-23';
var service_start_time = '10:00:00';
var service_end_time = '11:00:00';

DateTime currentDate = new DateTime.now();
DateTime times = DateTime.now();


  @override
  void initState() {
    super.initState();
    test();
  }

 test() {
    String currenttime = DateFormat('HH:mm').format(times);
    String currentdate = DateFormat('yyyy-mm-dd').format(currentDate);
    print(currenttime);    
    print(currentdate);
    
  }

So, basically i have start date and end date. I need to check current date is falling between these two dates or not.

like image 286
Roxx Avatar asked Oct 17 '20 14:10

Roxx


People also ask

How do you know if a date is between two dates flutter?

To check if a date is between 2 other ones in Flutter and Dart, you can use the isBefore() and isAfter() methods of the DateTime class.

How do you compare two dates in Dart flutter?

Simply use the methods isAfter() , isBefore() or isAtSameMomentAs() from DateTime . Other alternative, use compareTo(DateTime other) , as in the docs: Compares this DateTime object to [other], returning zero if the values are equal. Returns a negative value if this DateTime [isBefore] [other].

How do you compare two dates that are constructed differently in darts?

You just have to remove 'Z' from your String because when you print the date you can see the difference between two dates is only 'Z'. If you are getting String from some where else then you can use string methods to remove last 'Z' and then compare it. It is working fine when you do so.


2 Answers

You can check before/after using 'isBefore' and 'isAfter' in 'DateTime' class.
enter image description here

    DateTime startDate = DateTime.parse(service_start_date);
  DateTime endDate = DateTime.parse(service_end_date);
  
  DateTime now = DateTime.now();
  
  print('now: $now');
  print('startDate: $startDate');
  print('endDate: $endDate');
  print(startDate.isBefore(now));
  print(endDate.isAfter(now));
like image 80
KuKu Avatar answered Oct 21 '22 22:10

KuKu


I've made a series of extensions

extension DateTimeExtension on DateTime? {
  
  bool? isAfterOrEqualTo(DateTime dateTime) {
    final date = this;
    if (date != null) {
      final isAtSameMomentAs = dateTime.isAtSameMomentAs(date);
      return isAtSameMomentAs | date.isAfter(dateTime);
    }
    return null;
  }

  bool? isBeforeOrEqualTo(DateTime dateTime) {
    final date = this;
    if (date != null) {
      final isAtSameMomentAs = dateTime.isAtSameMomentAs(date);
      return isAtSameMomentAs | date.isBefore(dateTime);
    }
    return null;
  }

  bool? isBetween(
    DateTime fromDateTime,
    DateTime toDateTime,
  ) {
    final date = this;
    if (date != null) {
      final isAfter = date.isAfterOrEqualTo(fromDateTime) ?? false;
      final isBefore = date.isBeforeOrEqualTo(toDateTime) ?? false;
      return isAfter && isBefore;
    }
    return null;
  }

}

I'm hoping they're self explanatory but obviously you can call them like

DateTime.now().isBefore(yourDate)

DateTime.now().isAfter(yourDate)

DateTime.now().isBetween(fromDate, toDate)

like image 30
martinseal1987 Avatar answered Oct 21 '22 23:10

martinseal1987