nextDateTime static method

DateTime nextDateTime(
  1. DateTime min, [
  2. DateTime? max
])

Generates a random Date and time in the range 'min', 'max'. This method generate dates without time (or time set to 00:00:00)

  • min (optional) minimum range value
  • max max range value Returns a random Date and time value.

Implementation

/// - [min]   (optional) minimum range value
/// - [max]   max range value
/// Returns     a random Date and time value.

static DateTime nextDateTime(DateTime min, [DateTime? max]) {
  if (max == null) {
    max = min;
    min = DateTime(2000, 0, 1);
  }

  var diff = max.millisecondsSinceEpoch - min.millisecondsSinceEpoch;
  if (diff <= 0) return min;

  var time = min.millisecondsSinceEpoch + RandomInteger.nextInteger(0, diff);
  return DateTime.fromMillisecondsSinceEpoch(time).toUtc();
}