1

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

我怎样才能做到这一点。日期和时间非常简单。但是年月让我很难过。

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

4

4 回答 4

3

获得准确年数、月数和实际天数(因为Timespan DaysTotalDays是两个日期之间的天数)的最佳方法是分别使用AddYears,AddMonthsAddDays方法。

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

这是代码:

DateDiff 类:

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

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

测试代码:

    DateTime date1 = new DateTime(2012, 3, 1, 8, 0, 0);
    DateTime date2 = new DateTime(2013, 11, 4, 8, 0, 0);
    DateDiff dateDifference = new DateDiff(date1, date2);

    Console.WriteLine("Years = {0}, Months = {1}, Days = {2}", dateDifference.DiffYears, dateDifference.DiffMonths, dateDifference.DiffDays);
于 2013-11-04T00:17:52.533 回答
2

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

你可以做类似的事情

new DateTime(10,14,2012) - new DateTime(10,12,2012) ect..
于 2013-11-03T06:45:33.650 回答
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
于 2013-11-03T08:22:04.237 回答
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;
    }
}
于 2014-10-17T17:29:36.857 回答