From bd6d9795bf8b21693e61fd360bbb86969abed848 Mon Sep 17 00:00:00 2001 From: Maximilian Pfundstein Date: Fri, 4 Oct 2024 21:40:01 +0200 Subject: [PATCH] fix: calculate personnummer correctly before annual birthday --- Personnummer/Personnummer.cs | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/Personnummer/Personnummer.cs b/Personnummer/Personnummer.cs index af4689c..06dc544 100644 --- a/Personnummer/Personnummer.cs +++ b/Personnummer/Personnummer.cs @@ -24,7 +24,21 @@ public Options() public DateTime Date { get; private set; } - public int Age => DateTime.Now.Year - Date.Year; + public int Age + { + get + { + var now = DateTime.Now; + var age = now.Year - Date.Year; + + if (now.Month >= Date.Month && now.Day > Date.Day) + { + age--; + } + + return age; + } + } public string Separator => Age >= 100 ? "+" : "-";