isUnder13 method

bool isUnder13({
  1. DateTime? today,
})

Determines if the date (of birth) is under 13 years old.

This is useful for determining non-child content access based on the Children's Online Privacy Protection Act (COPPA).

today can be optionally provided to specify the date to compare against. If not provided, the current date is used.

NOTE: the legal definition of "under 13" can vary slightly by region or specific regulation.

Implementation

bool isUnder13({DateTime? today}) {
  today ??= DateTime.now();

  // NEW: Check if the date of birth is in the future.
  if (isAfter(today)) {
    return false; // Future dates are not considered under 13.
  }

  // Calculate the 13th birthday by adding 13 years to the current date
  final DateTime thirteenthBirthday = addYears(13);

  // Return true if today's date is before the 13th birthday,
  // indicating the age is under 13
  return today.isBefore(thirteenthBirthday);
}