【问题标题】:Year, Month, Days, hours between two dates年、月、日、两个日期之间的小时数
【发布时间】:2013-11-03 06:42:52
【问题描述】:

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

我该怎么做。日期和时间非常简单。但是年月让我很难过。

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

【问题讨论】:

  • 日期时间支持 +,-,,= 运算符...!你可以做 date1-date2
  • 当两个日期相隔 60 天时,Months 的值应该是多少?给我们一些输入示例和所需的匹配输出。
  • 我提供两个 DateTime 并生成一个字符串

标签: c#


【解决方案1】:

获得准确年数、月数和实际天数的最佳方法是使用AddYearsAddMonthsAddDays 方法。

我将在这里创建一个名为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);

【讨论】:

  • 能否为 GetMonths 和 GetDays 方法添加代码,因为我不知道该怎么做。
【解决方案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..

【讨论】:

  • 4 点赞? DateTime 没有构造函数,将 string 作为参数。您在发布答案之前尝试过吗?
  • 你是对的。更新以反映正确的参数。我是 Powershell Get-Date 的,它可以将字符串作为参数。
【解决方案3】:
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

【讨论】:

  • 所有月份都是 30 天吗?
  • @ErnodeWeerd 我们尝试计算不固定单位的差异(年 - 365\366,月 - 28\29\30\31),无论如何我们会有某种假设
  • @sh1ng - 是的,所以我们需要在回答问题之前得到符合要求的假设。
  • 我可以做到这一点,但我需要结果是 100% 准确的。
  • @Taufiq 是什么意思?
【解决方案4】:
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;
    }
}

【讨论】:

  • 在您的代码中添加一些 cmets 或解释会很有帮助。
  • 它和'sh1ng'一样,但是这个代码在两个日期之间添加了闰年的天数。还要加上 28 天和 31 天的月份天数。
猜你喜欢
  • 1970-01-01
  • 2014-04-07
  • 1970-01-01
  • 2021-02-28
  • 2022-10-02
  • 1970-01-01
  • 1970-01-01
  • 2016-07-30
  • 1970-01-01
相关资源
最近更新 更多