2013-01-12 244 views
1

我正在開發C#中的興趣計算器。我需要知道2 dates計算兩個日期之間的日曆月數

之間

天數所以,我計算是這樣的:

DateTime dt1 = DateTime.ParseExact(DateTime.Now.Date.ToString("dd-MM-yy"), "dd-MM-yy", CultureInfo.InvariantCulture).Date; 
    DateTime dt2 = DateTime.ParseExact(Duration, "dd-MM-yy", CultureInfo.InvariantCulture).Date; 

    Double no_days = (dt1 - dt2).TotalDays; 

然而,根據月份的天數會有所不同。因此,即使天數小於30,2月15日至3月15日也構成一個月。

任何ide我如何確定已過去的月數?利息計算在月結束時完成。

感謝

+1

尼特:發佈方式*不*顯示天的兩個日期之間的數字。 (雖然可能有一些TZ移的問題。) – 2013-01-12 06:12:56

+1

http://stackoverflow.com/questions/4638993/difference-in-months-between-two-dates –

回答

8

我想不出比這更清潔的方式:

((dt1.Year - dt2.Year) * 12) + (dt1.Month - dt2.Month) - (dt1.Day < dt2.Day?1:0); 

即使這樣,我不知道如果我也許錯過了一些邊緣情況。

+0

+1你是個聰明人。這只是取代了12行相當複雜的代碼。 –

0

我最終寫了自己的例程。我希望它能幫助別人。讓我知道是否有更簡單的方法來計算兩個日期之間的月份。

DateTime dt1 = DateTime.ParseExact(DateTime.Now.Date.ToString("dd-MM-yy"), "dd-MM-yy", CultureInfo.InvariantCulture).Date; 
DateTime dt2 = DateTime.ParseExact(Duration, "dd-MM-yy", CultureInfo.InvariantCulture).Date; 

DateTime temp = dt2; 
int no_months = 0; 
while ((dt1 - temp).TotalDays > 0) 
{ 
    temp = temp.AddMonths(1); 
    no_months++; 
} 

if (no_months > 0) 
{ 
    no_months = no_months - 1; 
} 

感謝

0
int MonthsElapsed = ((DateB.AddDays(1).Year - DateA.AddDays(1).Year) * 12) + 
        (DateB.AddDays(1).Month - DateA.AddDays(1).Month) - 
        (DateB.AddDays(1).Day < DateA.AddDays(1).Day ? 1 : 0); 

這個公式做一些假設:

  1. 整整一個月是DateA的一天,未來一個月的同一天之間的跨度。 (例如,1/15-2/15 = 1個月,1/15-3/15 = 2個月等)。我們將此稱爲觸發日期。
  2. 不包含觸發日期的任何月份的最後一天等同於觸發日期。 (1/28 ... 1/31到2/28都等於1個月。)
  3. 第一個過去的月份發生在1個整月後。 (當我進行利息計算時,第一個月的利息在DateA上累加,所以我在此公式中加上1)。

.AddDays(1)用於計算月末情景,其中a DateB不包含觸發日。我想不出一種更優雅的在線方法來解決這個問題。

1

這裏沒有很多明確的答案,因爲你總是在假裝東西。

該解決方案兩個日期假設您想要保存一個月作比較天之間的幾個月裏,(這意味着該月的一天,在計算時考慮)

例如,如果你有一個日期間計算2012年1月30日,2012年2月29日將不會是一個月,但2013年3月1日將會。

它已經測試了相當徹底,可能會後清理,因爲我們使用它,但在這裏:

private static int TotalMonthDifference(DateTime dtThis, DateTime dtOther) 
{ 
    int intReturn = 0; 
    bool sameMonth = false; 

    if (dtOther.Date < dtThis.Date) //used for an error catch in program, returns -1 
     intReturn--; 

    int dayOfMonth = dtThis.Day; //captures the month of day for when it adds a month and doesn't have that many days 
    int daysinMonth = 0; //used to caputre how many days are in the month 

    while (dtOther.Date > dtThis.Date) //while Other date is still under the other 
    { 
     dtThis = dtThis.AddMonths(1); //as we loop, we just keep adding a month for testing 
     daysinMonth = DateTime.DaysInMonth(dtThis.Year, dtThis.Month); //grabs the days in the current tested month 

     if (dtThis.Day != dayOfMonth) //Example 30 Jan 2013 will go to 28 Feb when a month is added, so when it goes to march it will be 28th and not 30th 
     { 
      if (daysinMonth < dayOfMonth) // uses day in month max if can't set back to day of month 
       dtThis.AddDays(daysinMonth - dtThis.Day); 
      else 
       dtThis.AddDays(dayOfMonth - dtThis.Day); 
     } 
     if (((dtOther.Year == dtThis.Year) && (dtOther.Month == dtThis.Month))) //If the loop puts it in the same month and year 
     { 
      if (dtOther.Day >= dayOfMonth) //check to see if it is the same day or later to add one to month 
       intReturn++; 
      sameMonth = true; //sets this to cancel out of the normal counting of month 
     } 
     if ((!sameMonth)&&(dtOther.Date > dtThis.Date))//so as long as it didn't reach the same month (or if i started in the same month, one month ahead, add a month) 
      intReturn++; 
    } 
    return intReturn; //return month 
} 
相關問題