在我的C#ASP.NET Core中,我试图获取两个日期(StartDate和EndDate(之间的Months、Quarters、BiAnnual(6个月(和Years。
我做了如下所示的TotalMonths,TotalQuarters,TotalYears及其工作:
public static int GetTotalMonth(DateTime startDate, DateTime endDate)
{
int totalMonth = 12 * (startDate.Year - endDate.Year) + startDate.Month - endDate.Month;
return Convert.ToInt32(Math.Abs(totalMonth));
}
public static int GetTotalQuarter(DateTime startDate, DateTime endDate)
{
int firstQuarter = getQuarter(startDate);
int secondQuarter = getQuarter(endDate);
return 1 + Math.Abs(firstQuarter - secondQuarter);
}
private static int getQuarter(DateTime date)
{
return (date.Year * 4) + ((date.Month - 1) / 3);
}
public static int GetTotalYear(DateTime startDate, DateTime endDate)
{
int years = endDate.Year - startDate.Year;
if (startDate.Month == endDate.Month &&
endDate.Day < startDate.Day)
{
years--;
}
else if (endDate.Month < startDate.Month)
{
years--;
}
return Math.Abs(years);
}
我遇到的问题是如何获得BiAnnual:
public static int GetTotalBiAnnual(DateTime startDate, DateTime endDate)
{
return;
}
我该怎么做?
在计算两个日期之间的差异时,通常使用TimeSpan
类。然而,这个类没有Month和Year,这导致我创建了一个名为TimeSpanWithYearAndMonth
的类。
public TimeSpanWithYearAndMonth(DateTime startDate, DateTime endDate)
{
var span = endDate - startDate;
TotalMonths = 12 * (endDate.Year - startDate.Year) + (endDate.Month - startDate.Month);
Years = TotalMonths / 12;
Months = TotalMonths - (Years * 12);
if (Months == 0 && Years == 0)
{
Days = span.Days;
}
else
{
var startDateExceptYearsAndMonths = startDate.AddYears(Years);
startDateExceptYearsAndMonths = startDateExceptYearsAndMonths.AddMonths(Months);
Days = (endDate - startDateExceptYearsAndMonths).Days;
}
Hours = span.Hours;
Minutes = span.Minutes;
Seconds = span.Seconds;
}
public int Minutes { get; }
public int Hours { get; }
public int Days { get; }
public int Years { get; }
public int Months { get; }
public int Seconds { get; }
public int TotalMonths { get; }
public int TotalHalfYears => TotalMonths / 6;
我用TotalHalfYears
("两年一次"(扩展了它,使用了一个简单的整数除法(当然,如果你愿意,你可以在现有的TotalMonths计算中只使用这一部分(。
下面是一些单元测试来演示它是如何工作的;
[TestFixture]
public class TimeSpanWithYearAndMonthTests
{
[Test]
public void Calculates_correctly()
{
new TimeSpanWithYearAndMonth(new DateTime(2019, 5, 1), new DateTime(2019, 5, 2)).Days.Should().Be(1);
new TimeSpanWithYearAndMonth(new DateTime(2019, 5, 28), new DateTime(2020, 5, 28)).Years.Should().Be(1);
new TimeSpanWithYearAndMonth(new DateTime(2019, 5, 28), new DateTime(2021, 5, 28)).Years.Should().Be(2);
new TimeSpanWithYearAndMonth(new DateTime(2019, 5, 28), new DateTime(2021, 5, 28)).Years.Should().Be(2);
new TimeSpanWithYearAndMonth(new DateTime(2019, 1, 1), new DateTime(2021, 1, 1)).TotalHalfYears.Should().Be(4);
new TimeSpanWithYearAndMonth(new DateTime(2019, 1, 1), new DateTime(2021, 1, 2)).TotalHalfYears.Should().Be(4);
new TimeSpanWithYearAndMonth(new DateTime(2019, 1, 1), new DateTime(2021, 6, 1)).TotalHalfYears.Should().Be(4);
new TimeSpanWithYearAndMonth(new DateTime(2019, 1, 1), new DateTime(2021, 7, 1)).TotalHalfYears.Should().Be(5);
var span = new TimeSpanWithYearAndMonth(new DateTime(2019, 5, 28), new DateTime(2021, 8, 28));
span.Years.Should().Be(2);
span.Months.Should().Be(3);
span.TotalMonths.Should().Be(27);
span = new TimeSpanWithYearAndMonth(new DateTime(2010, 5, 28), new DateTime(2020, 5, 29));
span.Years.Should().Be(10);
span.Months.Should().Be(0);
span.Days.Should().Be(1);
span.TotalMonths.Should().Be(120);
}
}
我假设从您现有的代码中,您需要计算startDate
和endDate
的半年期,并计算差异。代码可以类似于GetTotalQuarter
,就像这个
public static int GetTotalBiannual(DateTime startDate, DateTime endDate)
{
int first = GetBiannual(startDate);
int second = GetBiannual(endDate);
return 1 + Math.Abs(first - second);
}
private static int GetBiannual(DateTime date)
{
return (date.Year * 2) + ((date.Month - 1) / 6);
}
我建议您不要自己计算,而是使用NodaTime库。
然后你可以做这样的事情:
Period period = Period.Between(start, end, PeriodUnits.Months);
这将适用于不同的时间段。如果这对你来说更容易的话,你可以把它包装在你自己的课上。
对于您的BiAnnual,您可以将其扩展为周期Month值,并向下取整6个月的周期。
尝试
Math.Floor(GetTotalMonth(startDate, endDate) / 6)
您还编写了一个能完成一半工作的函数,请使用它;(