bchavez/Bogus · error · ArgumentOutOfRangeException
{nameof(year)} must be between 1854 and 2039.
Error message
{nameof(year)} must be between 1854 and 2039. What it means
ExtensionsForNorway.Fodselsnummer's individual-number generator covers only 1854-1899, 1900-1999, 2000-2039. Any other birth year throws ArgumentOutOfRangeException. These ranges reflect the real Norwegian fødselsnummer allocation rules.
Source
Thrown at Source/Bogus/Extensions/Norway/ExtensionsForNorway.cs:87
if (1854 <= year && year <= 1899)
{
from = 500;
to = 749;
}
else if (1900 <= year && year <= 1999)
{
from = 0;
to = 499;
}
else if (2000 <= year && year <= 2039)
{
from = 500;
to = 999;
}
else
{
throw new ArgumentOutOfRangeException(nameof(year), $"{nameof(year)} must be between 1854 and 2039.");
}
int individualNumber = gender == DataSets.Name.Gender.Female ? r.Even(from, to) : r.Odd(from, to);
return individualNumber.ToString("D3");
}
private static bool GenerateChecksum(string birthDate, string individualNumber, out string checksum)
{
int d1 = int.Parse(birthDate.Substring(0, 1));
int d2 = int.Parse(birthDate.Substring(1, 1));
int m1 = int.Parse(birthDate.Substring(2, 1));
int m2 = int.Parse(birthDate.Substring(3, 1));
int y1 = int.Parse(birthDate.Substring(4, 1));
int y2 = int.Parse(birthDate.Substring(5, 1));
int i1 = int.Parse(individualNumber.Substring(0, 1));
int i2 = int.Parse(individualNumber.Substring(1, 1));
int i3 = int.Parse(individualNumber.Substring(2, 1));View on GitHub (pinned to 6ece18c5c2)
Solutions
- Constrain Person.DateOfBirth to 1854-2039 before calling Fodselsnummer.
- Use f.Date.Between(new DateTime(1900,1,1), DateTime.Today) for realistic birth dates.
- Do not set DateOfBirth to dates beyond 2039 for Norwegian IDs.
Example fix
// before person.DateOfBirth = new DateTime(1850, 1, 1); var fn = person.Fodselsnummer(); // after person.DateOfBirth = f.Date.Between(new DateTime(1920,1,1), DateTime.Today); var fn = person.Fodselsnummer();
Defensive patterns
Strategy: validation
Validate before calling
int year = person.DateOfBirth.Year;
if (year < 1854 || year > 2039)
throw new ArgumentOutOfRangeException(nameof(year), "Norwegian fødselsnummer requires 1854-2039.");
var fn = person.Fodselsnummer(); Type guard
static bool NorwegianYearOk(int y) => y >= 1854 && y <= 2039;
Prevention
- Keep birth years within 1854-2039 for Norwegian IDs.
- Do not push DateOfBirth beyond 2039 even for future-dated fixtures.
- Generate dates with f.Date.Between(new DateTime(1900,1,1), DateTime.Today).
When it happens
Trigger: Calling person.Fodselsnummer() on a Person whose DateOfBirth.Year is below 1854 or above 2039.
Common situations: Setting DateOfBirth to a date outside the supported era (e.g. a 2100 future-date fixture) and then generating a Norwegian national ID.
Related errors
- {nameof(year)} must be between 1858 and 2057.
- PESEL for the year below 1800 is invalid.
- PESEL for year above 2300 is invalid.
- {nameof(p.DateOfBirth.Year)} must be between 1900 and 2099.
- Gender not handled.
AI-assisted analysis of bchavez/Bogus@6ece18c5c2 (2026-08-13).
Data as JSON: /api/errors/57dad62aef2e97c4.
Report an issue: GitHub.