78 lines
2.1 KiB
C#
78 lines
2.1 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Text;
|
|
|
|
namespace BS.Shared.Core
|
|
{
|
|
public static class DateTimeUtils
|
|
{
|
|
public static AnzahlMonate GetTotalMonths(DateTime start, DateTime end)
|
|
{
|
|
int restTageStart = 0;
|
|
int restTageEnd = 0;
|
|
|
|
DateTime startDt = start;
|
|
DateTime endDt = end;
|
|
|
|
if (startDt.Day > 1)
|
|
{
|
|
var endMonat = new DateTime(startDt.Year, startDt.Month, 1).AddMonths(1).AddDays(-1);
|
|
|
|
if (endDt < endMonat)
|
|
{
|
|
endMonat = endDt;
|
|
}
|
|
restTageStart = endMonat.Subtract(startDt).Days + 1;
|
|
startDt = endMonat.AddDays(1);
|
|
}
|
|
if (startDt <= endDt && endDt.AddDays(1).Day != 1)
|
|
{
|
|
var startMonat = new DateTime(endDt.Year, endDt.Month, 1);
|
|
|
|
restTageEnd += endDt.Subtract(startMonat).Days + 1;
|
|
|
|
endDt = startMonat;
|
|
}
|
|
|
|
int fullMonths = 0;
|
|
while (startDt < endDt)
|
|
{
|
|
fullMonths++;
|
|
startDt = startDt.AddMonths(1);
|
|
}
|
|
|
|
decimal monthsTotal = fullMonths;
|
|
|
|
|
|
if (restTageStart != 0)
|
|
{
|
|
monthsTotal += restTageStart / (decimal)DateTime.DaysInMonth(start.Date.Year, start.Date.Month);
|
|
|
|
}
|
|
if (restTageEnd != 0)
|
|
{
|
|
monthsTotal += restTageEnd / (decimal)DateTime.DaysInMonth(end.Date.Year, end.Date.Month);
|
|
|
|
}
|
|
|
|
var am = new AnzahlMonate();
|
|
am.GanzeMonate = fullMonths;
|
|
am.RestTageAnfang = restTageStart;
|
|
am.RestTageEnde = restTageEnd;
|
|
am.AnzahlMonateGesamt = monthsTotal;
|
|
|
|
return am;
|
|
}
|
|
|
|
}
|
|
|
|
public class AnzahlMonate
|
|
{
|
|
public decimal AnzahlMonateGesamt { get; set; }
|
|
public int GanzeMonate { get; set; }
|
|
public decimal RestTageAnfang { get; set; }
|
|
public decimal RestTageEnde { get; set; }
|
|
}
|
|
}
|