两个日期之间的年,月,日,小时

时间:2013-11-03 06:42:53

标签: c#

我需要找到两个日期之间的差异并显示结果 年,月,日和小时格式,例如1年2个月6天4小时。

我该怎么办?日和小时非常简单。但是一年又一个月让我很难受。

我需要结果100%准确...我们不能假设每月30天或每年356。 请帮忙谢谢。

4 个答案:

答案 0 :(得分:3)

获得准确的年数,月数和实际天数(因为Timespan DaysTotalDays是两天之间的天数)的最佳方法是使用{分别为{1}},AddYearsAddMonths方法。

我将在这里创建一个名为AddDays的类,它将计算两个日期之间的年,月和日数。但是,我只会给你计算年份差异的代码(和算法),因为如果你知道岁月,你也会知道如何做几个月和几天。当然,这样你自己也可以继续工作; - )

以下是代码:

DateDiff类:

DateDiff

您可以像这样测试Main中的代码:

测试代码:

class DateDiff
 {
    public DateDiff(DateTime startDate, DateTime endDate)
    {
        GetYears(startDate, endDate); // Get the Number of Years Difference between two dates
        GetMonths(startDate.AddYears(YearsDiff), endDate); // Getting the Number of Months Difference but using the Years difference earlier
        GetDays(startDate.AddYears(YearsDiff).AddMonths(MonthsDiff), endDate); // Getting the Number of Days Difference but using Years and Months difference earlier
    }
    void GetYears(DateTime startDate, DateTime endDate)
    {
        int Years = 0;
        // Traverse until start date parameter is beyond the end date parameter
        while (endDate.CompareTo(startDate.AddYears(++Years))>=0) {}
        YearsDiff = --Years; // Deduct the extra 1 Year and save to YearsDiff property
    }
    void GetMonths(DateTime startDate, DateTime endDate)
    {
      // Provide your own code here
    }
    void GetDays(DateTime startDate, DateTime endDate)
    {
      // Provided your own code here
    }

    public int YearsDiff { get; set; }
    public int MonthsDiff { get; set; }
    public int DaysDiff { get; set; }
 }

答案 1 :(得分:2)

查看DateTime:http://msdn.microsoft.com/en-us/library/system.datetime(v=vs.110).aspx

您可以执行

之类的操作
new DateTime(10,14,2012) - new DateTime(10,12,2012) ect..

答案 2 :(得分:1)

var timeSpan = dateTime2 - dateTime1;
var years = timeSpan.Days / 365;
var months = (timeSpan.Days - years * 365)/30;
var days = timeSpan.Days - years * 365 - months * 30;
// and so on

答案 3 :(得分:0)

class Program
{
    static void Main()
    {
        DateTime oldDate = new DateTime(2014,1,1);
        DateTime newDate = DateTime.Now;
        TimeSpan dif = newDate - oldDate;

        int leapdays = GetLeapDays(oldDate, newDate);

        var years = (dif.Days-leapdays) / 365;
        int otherdays = GetAnOtherDays(oldDate, newDate , years);
        int months = (int)((dif.Days - (leapdays + otherdays)- (years * 365)) / 30);
        int days = (int)(dif.Days - years * 365 - months * 30) - (leapdays + otherdays);

        Console.WriteLine("Edad es {0} años, {1} meses, {2} días", years, months, days) ;
        Console.ReadLine();
    }

    public static int GetAnOtherDays(DateTime oldDate, DateTime newDate, int years) {
        int days = 0;

        oldDate = oldDate.AddYears(years);
        DateTime oldDate1 = oldDate.AddMonths(1);

        while ((oldDate1.Month <= newDate.Month && oldDate1.Year<=newDate.Year) || 
            (oldDate1.Month>newDate.Month && oldDate1.Year<newDate.Year)) {
            days += ((TimeSpan)(oldDate1 - oldDate)).Days - 30;
            oldDate = oldDate.AddMonths(1);
            oldDate1 = oldDate.AddMonths(1);
        }

        return days;
    }

    public static int GetLeapDays(DateTime oldDate, DateTime newDate)
    {
        int days = 0;

        while (oldDate.Year < newDate.Year) {
            if (DateTime.IsLeapYear(oldDate.Year)) days += 1;
            oldDate = oldDate.AddYears(1);
        }

        return days;
    }
}