bchavez/Bogus · error · ArgumentOutOfRangeException

{nameof(year)} must be between 1858 and 2057.

Error message

{nameof(year)} must be between 1858 and 2057.

What it means

GenerateIndividualFourDigitNumber (used by Person.Cpr when validChecksum=false) maps a birth year to an individual-number range and only covers 1858-1899, 1900-1936, 1937-1999, 2000-2036, 2037-2057. Any other year throws ArgumentOutOfRangeException. This is the Danish CPR numbering scheme's own era limits, not an arbitrary Bogus choice.

Source

Thrown at Source/Bogus/Extensions/Denmark/ExtensionsForDenmark.cs:108

            break;
         case >= 1900 and <= 1936:
            from = 0;
            to = 3999;
            break;
         case >= 1937 and <= 1999:
            from = 0;
            to = 4999;
            break;
         case >= 2000 and <= 2036:
            from = 4000;
            to = 9999;
            break;
         case >= 2037 and <= 2057:
            from = 5000;
            to = 9999;
            break;
         default:
            throw new ArgumentOutOfRangeException(nameof(year), $"{nameof(year)} must be between 1858 and 2057.");
      }

      int individualNumber = gender == DataSets.Name.Gender.Female ? r.Even(from, to) : r.Odd(from, to);

      return individualNumber.ToString("D4");
   }

   private static string GenerateIndividualThreeDigitNumber(Randomizer r, int year)
   {
      int from;
      int to;

      switch( year )
      {
         case >= 1858 and <= 1899:
            from = 500;
            to = 899;
            break;

View on GitHub (pinned to 6ece18c5c2)

Solutions

  1. Constrain Person.DateOfBirth to a year between 1858 and 2057 before calling Cpr.
  2. Generate the birth date via the Date dataset, e.g. f.Date.Between(new DateTime(1900,1,1), DateTime.Today).
  3. Avoid hand-setting DateOfBirth to dates outside the supported era.

Example fix

// before
person.DateOfBirth = new DateTime(2100, 1, 1);
var cpr = person.Cpr(validChecksum: false);

// after
person.DateOfBirth = f.Date.Between(new DateTime(1960,1,1), DateTime.Today);
var cpr = person.Cpr(validChecksum: false);
Defensive patterns

Strategy: validation

Validate before calling

int year = person.DateOfBirth.Year;
if (year < 1858 || year > 2057)
    throw new ArgumentOutOfRangeException(nameof(year), "Danish CPR requires 1858-2057.");
var cpr = person.Cpr(validChecksum: false);

Type guard

static bool DanishCprYearOk(int y) => y >= 1858 && y <= 2057;

Prevention

When it happens

Trigger: Calling person.Cpr(validChecksum: false) on a Person whose DateOfBirth.Year is below 1858 or above 2057.

Common situations: Overriding Person.DateOfBirth to a far-future date (e.g. DateTime.Now.AddYears(50)) or a pre-1858 date for historical fixtures, then generating a Danish CPR.

Related errors


AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13). Data as JSON: /api/errors/7e96ffa238233de4. Report an issue: GitHub.