DateTime dayStart;
DateTime dateEnd;
TimeSpan ts = dateEnt - dateStart;
打印:...年(S)和...一个月(县)如何找到两个日期之间的年份和月份差异?
我该怎么计算呢?
的.NET Framework 2.0
C#
asp.net项目。
DateTime dayStart;
DateTime dateEnd;
TimeSpan ts = dateEnt - dateStart;
打印:...年(S)和...一个月(县)如何找到两个日期之间的年份和月份差异?
我该怎么计算呢?
的.NET Framework 2.0
C#
asp.net项目。
这取决于你想要精确计算的东西。
随着年份和月份的长短的变化,您无法将TimeSpan
中的值翻译成确切的年份和月份。你可以计算出近似的年月是这样的:
int years = ts.Days/365;
int months = (ts.Days % 365)/31;
如果你想准确的区别,你有比较DateTime
值。
You should first read this article from Jon Skeet,特别是从文字“介绍周期和周期算术”,它会让你感兴趣。
所以,你必须定义在一定时期是在一个月的变化,一年等
Noda-time已经包含了很多的这个功能。但我认为它还没有发布。
Gah,如果其他人为我提出这个问题,我有什么意见可以提到野田时间? ;) – 2011-01-20 14:36:48
我觉得这样的事情会做到这一点:
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个月,一年(至少在我们的日历中)。
下面将计算年,月,日
日期时间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);
您需要(很多)定义在这里。 2011年1月31日至2011年2月1日之间有几个月?如果你想分数,一个月多久? – 2011-01-20 14:41:39