Created
December 28, 2021 14:36
-
-
Save mortenmoulder/60898f8bcd7e41556fb4a689f88a8ef2 to your computer and use it in GitHub Desktop.
Beregn lønningsdage ud fra årstal
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Beregn lønningsdage ud fra et årstal | |
* | |
* Den tager højde for officielle banklukkedage, | |
* som kun kan påvirkes af Store bededag og Nytårsdag, | |
* da disse datoer kan lande på sidste hverdag på en måned, | |
* såsom Store bededag 2021 som er fredag d. 30. april | |
*/ | |
using System.Globalization; | |
CultureInfo.CurrentCulture = new CultureInfo("da-DK"); | |
var year = 2021; | |
for (var i = 1; i <= 12; i++) | |
{ | |
var date = new DateTime(year, i, 1); | |
date = date.AddMonths(1).AddDays(-1); | |
date = GetValidDate(date); | |
Console.WriteLine($"{date:dddd} den {date:dd. MMM yyyy}"); | |
} | |
DateTime GetValidDate(DateTime date) | |
{ | |
var dayOfWeek = (int)date.DayOfWeek; | |
var easter = GetCatholicEaster(date.Year); | |
var bigPrayerDay = easter.AddDays(21).AddDays(5); //add 3 weeks and 5 days | |
var newYear = new DateTime(date.Year, 12, 31); | |
if ( | |
dayOfWeek == 0 || | |
dayOfWeek == 6 || | |
date.Date == bigPrayerDay.Date || | |
date.Date == newYear.Date | |
) | |
{ | |
date = date.AddDays(-1); | |
date = GetValidDate(date); | |
} | |
return date; | |
} | |
DateTime GetCatholicEaster(int year) | |
{ | |
int month = 3; | |
int G = year % 19 + 1; | |
int C = year / 100 + 1; | |
int X = (3 * C) / 4 - 12; | |
int Y = (8 * C + 5) / 25 - 5; | |
int Z = (5 * year) / 4 - X - 10; | |
int E = (11 * G + 20 + Y - X) % 30; | |
if (E == 24) | |
{ | |
E++; | |
} | |
if ((E == 25) && (G > 11)) | |
{ | |
E++; | |
} | |
int N = 44 - E; | |
if (N < 21) | |
{ | |
N = N + 30; | |
} | |
int P = (N + 7) - ((Z + N) % 7); | |
if (P > 31) | |
{ | |
P = P - 31; | |
month = 4; | |
} | |
return new DateTime(year, month, P); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment