如何找到两个日期之间的年月差异?

时间:2022-06-19 14:18:13
DateTime dayStart;
DateTime dateEnd;

TimeSpan ts = dateEnt - dateStart;

Print : ... Year(s) and ... Month(s)

打印:...年份和...月份

how can I calculate it?

我怎么计算呢?

.net framework 2.0
c#

.net framework 2.0 c#

asp.net project.

4 个解决方案

#1


3  

That depends on what you want to calculate exactly.

这取决于您想要准确计算的内容。

You can't translate the value in a TimeSpan to exact years and months, as the length of years and months varies. You can calculate approximate years and months like this:

您无法将TimeSpan中的值转换为精确的年份和月份,因为年份和月份的长度会有所不同。您可以像这样计算大约的年数和月数:

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

If you want the exact difference, you have to compare the DateTime values.

如果您想要精确的差异,则必须比较DateTime值。

#2


7  

You should first read this article from Jon Skeet, specially from the text "Introducing periods and period arithmetic" it gets interesting for you.

您应该首先阅读Jon Skeet撰写的这篇文章,特别是文章“Introducing period and period arithmetic”,它会让您感兴趣。

So, you have to define when a certain period is a change in month, in year etc.

因此,您必须定义某个时段是月份,年份等的变化。

Noda-time already contains a lot of functions for this. But I don't think it is released yet.

Noda-time已经包含了很多功能。但我不认为它已经发布了。

#3


4  

Following will calculate the age in years, months, days

以下将计算年,月,日的年龄

        DateTime dob = "10/18/1981";  // date of birth
        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);

#4


3  

I think something like this would do it:

我认为这样的事情会这样做:

  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);

Edit To clarify: There's no need for complicated date algorhitms or any of that kind of stuff, because there are always 12 months in a year (at least in our calendar).

编辑澄清:不需要复杂的日期算法或任何类型的东西,因为一年中总有12个月(至少在我们的日历中)。

#1


3  

That depends on what you want to calculate exactly.

这取决于您想要准确计算的内容。

You can't translate the value in a TimeSpan to exact years and months, as the length of years and months varies. You can calculate approximate years and months like this:

您无法将TimeSpan中的值转换为精确的年份和月份,因为年份和月份的长度会有所不同。您可以像这样计算大约的年数和月数:

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

If you want the exact difference, you have to compare the DateTime values.

如果您想要精确的差异,则必须比较DateTime值。

#2


7  

You should first read this article from Jon Skeet, specially from the text "Introducing periods and period arithmetic" it gets interesting for you.

您应该首先阅读Jon Skeet撰写的这篇文章,特别是文章“Introducing period and period arithmetic”,它会让您感兴趣。

So, you have to define when a certain period is a change in month, in year etc.

因此,您必须定义某个时段是月份,年份等的变化。

Noda-time already contains a lot of functions for this. But I don't think it is released yet.

Noda-time已经包含了很多功能。但我不认为它已经发布了。

#3


4  

Following will calculate the age in years, months, days

以下将计算年,月,日的年龄

        DateTime dob = "10/18/1981";  // date of birth
        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);

#4


3  

I think something like this would do it:

我认为这样的事情会这样做:

  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);

Edit To clarify: There's no need for complicated date algorhitms or any of that kind of stuff, because there are always 12 months in a year (at least in our calendar).

编辑澄清:不需要复杂的日期算法或任何类型的东西,因为一年中总有12个月(至少在我们的日历中)。