2011-01-20 155 views
6
DateTime dayStart; 
DateTime dateEnd; 

TimeSpan ts = dateEnt - dateStart; 

打印:...年(S)和...一個月(縣)如何找到兩個日期之間的年份和月份差異?

我該怎麼計算呢?

的.NET Framework 2.0
C#

asp.net項目。

+0

您需要(很多)定義在這裏。 2011年1月31日至2011年2月1日之間有幾個月?如果你想分數,一個月多久? – 2011-01-20 14:41:39

回答

3

這取決於你想要精確計算的東西。

隨着年份和月份的長短的變化,您無法將TimeSpan中的值翻譯成確切的年份和月份。你可以計算出近似的年月是這樣的:

int years = ts.Days/365; 
int months = (ts.Days % 365)/31; 

如果你想準確的區別,你有比較DateTime值。

7

You should first read this article from Jon Skeet,特別是從文字「介紹週期和週期算術」,它會讓你感興趣。

所以,你必須定義在一定時期是在一個月的變化,一年等

Noda-time已經包含了很多的這個功能。但我認爲它還沒有發佈。

+1

Gah,如果其他人爲我提出這個問題,我有什麼意見可以提到野田時間? ;) – 2011-01-20 14:36:48

2

我覺得這樣的事情會做到這一點:

DateTime date1 = new DateTime(1973, 07, 20); 
    DateTime date2 = new DateTime(2010, 01, 10); 

    // Swap them if one is bigger than the other 
    if (date2 < date1) 
    { 
    DateTime date3 = date2; 
    date2 = date1; 
    date1 = date3; 
    } 

    // Now date2 >= date1. 

    TimeSpan ts = date2 - date1; 

    // Total days 
    Console.WriteLine(ts.TotalDays); 

    // Total years 
    int years = date2.Year - date1.Year; 

    int months = 0; 
    // Total monts 
    if (date2.Month < date1.Month) 
    { 
    // example: March 2010 (3) and January 2011 (1); this should be 10 monts 
    // 12 - 3 + 1 = 10 
    // Take the 12 months of a year into account 
    months = 12 - date1.Month + date2.Month; 
    } 
    else 
    { 
    months = date2.Month - date1.Month; 
    } 
    Console.WriteLine("Years: {0}, Months: {1}", years, months); 

編輯澄清:有沒有需要複雜的日期algorhitms或那樣的東西,因爲總有12個月,一年(至少在我們的日曆中)。

3

下面將計算年,月,日

日期時間DOB = 「1981年10月18日」 時代; //出生日期 DateTime now = DateTime.Now;

 // Swap them if one is bigger than the other 
     if (now < dob) 
     { 
      DateTime date3 = now; 
      now = dob; 
      dob = date3; 
     } 
     TimeSpan ts = now - dob; 
     //Debug.WriteLine(ts.TotalDays); 

     int years = 0; 
     int months = 0, days=0; 
     if ((now.Month <= dob.Month) && (now.Day < dob.Day)) // i.e. now = 03Jan15, dob = 23dec14 
     { 
      // example: March 2010 (3) and January 2011 (1); this should be 10 months. // 12 - 3 + 1 = 10 
      years = now.Year - dob.Year-1; 
      months = 12 - dob.Month + now.Month-1; 
      days = DateTime.DaysInMonth(dob.Year, dob.Month) - dob.Day + now.Day; 

      if(months==12) 
      { 
       months=0; 
       years +=1; 
      } 
     } 
     else if ((now.Month <= dob.Month) && (now.Day >= dob.Day)) // i.e. now = 23Jan15, dob = 20dec14 
     { 
      // example: March 2010 (3) and January 2011 (1); this should be 10 months. // 12 - 3 + 1 = 10 
      years = now.Year - dob.Year - 1; 
      months = 12 - dob.Month + now.Month; 
      days = now.Day - dob.Day; 
      if (months == 12) 
      { 
       months = 0; 
       years += 1; 
      } 
     } 
     else if ((now.Month > dob.Month) && (now.Day < dob.Day)) // i.e. now = 18oct15, dob = 22feb14 
     { 
      years = now.Year - dob.Year; 
      months = now.Month - dob.Month-1; 
      days = DateTime.DaysInMonth(dob.Year, dob.Month) - dob.Day + now.Day; 
     } 
     else if ((now.Month > dob.Month) && (now.Day >= dob.Day)) // i.e. now = 22oct15, dob = 18feb14 
     { 
      years = now.Year - dob.Year; 
      months = now.Month - dob.Month; 
      days = now.Day - dob.Day; 
     } 
     Debug.WriteLine("Years: {0}, Months: {1}, Days: {2}", years, months, days); 
相關問題