gpt4 book ai didi

c# - 如何使用 C# 获取两个日期之间的 BiAnnual。网

转载 作者:行者123 更新时间:2023-12-05 04:36:36 25 4
gpt4 key购买 nike

在我的 C# ASP.NET Core 中,我试图获取两个日期(StartDate 和 EndDate)之间的月份、季度、双年度(6 个月的数量)和年份。

我已经完成了如下所示的 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 类。然而,这个类没有月和年,这让我创建了一个名为 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(“bi annual”)扩展了它(如果愿意,您当然可以只将这部分用于现有的 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);
}
}

关于c# - 如何使用 C# 获取两个日期之间的 BiAnnual。网,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70798323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com