在我的 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;
}
我該怎么做?
uj5u.com熱心網友回復:
在計算兩個日期之間的差異時,您通常使用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);
}
}
uj5u.com熱心網友回復:
我假設從您現有的代碼中,您需要計算其中和所在的半年度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);
}
uj5u.com熱心網友回復:
我建議您不要自己計算,而是使用 NodaTime 庫。
然后你可以做這樣的事情:
Period period = Period.Between(start, end, PeriodUnits.Months);
然后,這將適用于各種不同的時間段。如果對您來說更容易,您可以將其包裝在您自己的類中。
對于您的 BiAnnual,您可以擴展它以獲取期間 Month 值并向下舍入 6 個月期間。
uj5u.com熱心網友回復:
嘗試
Math.Floor(GetTotalMonth(startDate, endDate) / 6)
您還撰寫了一個可以完成一半作業的函式,請使用它;)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/419197.html
標籤:
