杨魅力
这实际上非常棘手。不同的总天数可能会产生相同的结果。例如:2008年6月19日至2010年6月19日= 2年,也是365 * 2天2006年6月19日至2008年6月19日= 2年,但由于闰年也是365 + 366天你可能想要减去年数,直到你得到两个相隔不到一年的日期。然后减去几个月,直到你得到两个相隔不到一个月的日期。进一步混淆:当你可能以“3月30日”开始时减去(或增加)月份是棘手的 - 比这更早一个月?更进一步的混乱(可能不相关):即使一天也不总是24小时。夏令时任何人?更进一步的混乱(几乎肯定不相关):即使一分钟也不总是60秒。闰秒非常令人困惑......我现在没有时间找出正确的方法 - 这个答案主要是为了提出这个事实并不像听起来那么简单。编辑:不幸的是,我没有足够的时间来完全回答这个问题。我建议你先定义一个代表一个结构的结构Period:public struct Period{
private readonly int days;
public int Days { get { return days; } }
private readonly int months;
public int Months { get { return months; } }
private readonly int years;
public int Years { get { return years; } }
public Period(int years, int months, int days)
{
this.years = years;
this.months = months;
this.days = days;
}
public Period WithDays(int newDays)
{
return new Period(years, months, newDays);
}
public Period WithMonths(int newMonths)
{
return new Period(years, newMonths, days);
}
public Period WithYears(int newYears)
{
return new Period(newYears, months, days);
}
public static DateTime operator +(DateTime date, Period period)
{
// TODO: Implement this!
}
public static Period Difference(DateTime first, DateTime second)
{
// TODO: Implement this!
}}我建议你首先实现+运算符,它应该通知Difference方法 - 你应该确保first + (Period.Difference(first, second)) == second所有first/ second值。首先编写一系列单元测试 - 最初是“简单”的案例,然后转向涉及闰年的棘手问题。我知道正常的方法是一次编写一个测试,但在开始任何实现工作之前,我会亲自集体讨论它们。让自己有一天正确实施。这是棘手的事情。请注意,我在这里省略了几周 - 这个值至少很容易,因为它总是7天。因此,给定(正)期间,您将拥有:int years = period.Years;int months = period.Months;int weeks = period.Days / 7;int daysWithinWeek = period.Days % 7;(我建议你甚至不要考虑消极时期 - 确保一切都是正面的。)
动漫人物
多年来/月/周的正确差计算,日历中的CultureInfo的必须考虑:飞跃与非闰年不同天数的月份具有不同周数的年份(根据一周的第一天和日历周规则而变化).NET时间段库的DateDiff类尊重所有这些因素:// ----------------------------------------------------------------------public void DateDiffSample(){
DateTime date1 = new DateTime( 2009, 11, 8, 7, 13, 59 );
Console.WriteLine( "Date1: {0}", date1 );
// > Date1: 08.11.2009 07:13:59
DateTime date2 = new DateTime( 2011, 3, 20, 19, 55, 28 );
Console.WriteLine( "Date2: {0}", date2 );
// > Date2: 20.03.2011 19:55:28
DateDiff dateDiff = new DateDiff( date1, date2 );
// differences
Console.WriteLine( "DateDiff.Years: {0}", dateDiff.Years );
// > DateDiff.Years: 1
Console.WriteLine( "DateDiff.Quarters: {0}", dateDiff.Quarters );
// > DateDiff.Quarters: 5
Console.WriteLine( "DateDiff.Months: {0}", dateDiff.Months );
// > DateDiff.Months: 16
Console.WriteLine( "DateDiff.Weeks: {0}", dateDiff.Weeks );
// > DateDiff.Weeks: 70
Console.WriteLine( "DateDiff.Days: {0}", dateDiff.Days );
// > DateDiff.Days: 497
Console.WriteLine( "DateDiff.Weekdays: {0}", dateDiff.Weekdays );
// > DateDiff.Weekdays: 71
Console.WriteLine( "DateDiff.Hours: {0}", dateDiff.Hours );
// > DateDiff.Hours: 11940
Console.WriteLine( "DateDiff.Minutes: {0}", dateDiff.Minutes );
// > DateDiff.Minutes: 716441
Console.WriteLine( "DateDiff.Seconds: {0}", dateDiff.Seconds );
// > DateDiff.Seconds: 42986489
// elapsed
Console.WriteLine( "DateDiff.ElapsedYears: {0}", dateDiff.ElapsedYears );
// > DateDiff.ElapsedYears: 1
Console.WriteLine( "DateDiff.ElapsedMonths: {0}", dateDiff.ElapsedMonths );
// > DateDiff.ElapsedMonths: 4
Console.WriteLine( "DateDiff.ElapsedDays: {0}", dateDiff.ElapsedDays );
// > DateDiff.ElapsedDays: 12
Console.WriteLine( "DateDiff.ElapsedHours: {0}", dateDiff.ElapsedHours );
// > DateDiff.ElapsedHours: 12
Console.WriteLine( "DateDiff.ElapsedMinutes: {0}", dateDiff.ElapsedMinutes );
// > DateDiff.ElapsedMinutes: 41
Console.WriteLine( "DateDiff.ElapsedSeconds: {0}", dateDiff.ElapsedSeconds );
// > DateDiff.ElapsedSeconds: 29
// description
Console.WriteLine( "DateDiff.GetDescription(1): {0}", dateDiff.GetDescription( 1 ) );
// > DateDiff.GetDescription(1): 1 Year
Console.WriteLine( "DateDiff.GetDescription(2): {0}", dateDiff.GetDescription( 2 ) );
// > DateDiff.GetDescription(2): 1 Year 4 Months
Console.WriteLine( "DateDiff.GetDescription(3): {0}", dateDiff.GetDescription( 3 ) );
// > DateDiff.GetDescription(3): 1 Year 4 Months 12 Days
Console.WriteLine( "DateDiff.GetDescription(4): {0}", dateDiff.GetDescription( 4 ) );
// > DateDiff.GetDescription(4): 1 Year 4 Months 12 Days 12 Hours
Console.WriteLine( "DateDiff.GetDescription(5): {0}", dateDiff.GetDescription( 5 ) );
// > DateDiff.GetDescription(5): 1 Year 4 Months 12 Days 12 Hours 41 Mins
Console.WriteLine( "DateDiff.GetDescription(6): {0}", dateDiff.GetDescription( 6 ) );
// > DateDiff.GetDescription(6): 1 Year 4 Months 12 Days 12 Hours 41 Mins 29 Secs} // DateDiffSampleDateDiff还计算Quarters的差异。