Flutter:检查日期是否在两个日期之间

byqmnocz  于 2023-04-22  发布在  Flutter
关注(0)|答案(4)|浏览(173)

我需要检查日期是否在两个日期之间。
我试着搜索了一下,但没有结果。可能你见过这样的场景。所以,寻求你的建议。
这是我的代码。

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);
    
  }

所以,基本上我有开始日期和结束日期。我需要检查当前日期是否介于这两个日期之间。

fykwrbwg

fykwrbwg1#

您可以使用“DateTime”类中的“isBefore”和“isAfter”检查之前/之后。

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));
3hvapo4f

3hvapo4f2#

我做了一系列的扩展

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;
  }

}

我希望它们是自我解释的,但显然你可以这样称呼它们
DateTime.now().isBefore(yourDate)
DateTime.now().isAfter(yourDate)
DateTime.now().isBetween(fromDate,toDate)

hvvq6cgz

hvvq6cgz3#

不要忘记检查这一天是否与两个日期中的一个相同,也可以在条件ex中添加一个或:if(start is before now||(start.month==now.month &&start.day==now.day ...etc)

wj8zmpe1

wj8zmpe14#

由于@KuKu的答案基本上是正确的,但将设置为排他性的边界,我想提出一个抛光版本的@martinseal1987的答案。

extension DateTimeExtension on DateTime {
  bool isAfterOrEqual(DateTime other) {
    return isAtSameMomentAs(other) || isAfter(other);
  }

  bool isBeforeOrEqual(DateTime other) {
    return isAtSameMomentAs(other) || isBefore(other);
  }
  
  bool isBetween({required DateTime from, required DateTime to}) {
    return isAfterOrEqual(from) && isBeforeOrEqual(to);
  }
  
  bool isBetweenExclusive({required DateTime from, required DateTime to}) {
    return isAfter(from) && isBefore(to);
  }
}

// Test cases
void main() {
  final now = DateTime.now();
  final after = now.add(Duration(days: 1));
  final before = now.subtract(Duration(days: 1));
  
  print(now.isAfterOrEqual(now)); // true
  print(now.isBeforeOrEqual(now)); // true
  
  print(now.isAfterOrEqual(before)); // true
  print(now.isAfterOrEqual(after)); // false
  
  print(now.isBeforeOrEqual(after)); // true
  print(now.isBeforeOrEqual(before)); // false
  
  print(now.isBetween(from: before, to: after)); // true
  print(now.isBetween(from: now, to: now)); // true
  print(now.isBetween(from: before, to: now)); // true
  print(now.isBetween(from: now, to: after)); // true
  
  print(now.isBetweenExclusive(from: before, to: now)); // false
  print(now.isBetweenExclusive(from: now, to: after)); // false
}

相关问题