【问题标题】:Difference between two dates in Vb.net (0 YEAR, 0 MONTHS, 0 DAYS LEFT) [duplicate]Vb.net中两个日期之间的差异(0 YEAR,0 MONTHS,0 DAYS LEFT)[重复]
【发布时间】:2011-11-29 06:33:37
【问题描述】:

可能重复:
How to get difference between two dates in Year/Month/Week/Day?

我对两个日期之间的差异有疑问。 我需要输出 0 YEAR, 0 MONTHS, 0 DAYS LEFTm 例如:

 1 YEAR, 2 MONTHS, 3 DAYS LEFT

使用 dateDiff 函数或使用其他任何东西是不可能的。

【问题讨论】:

    标签: .net vb.net


    【解决方案1】:

    使用 DateTime 作为表示,你可以有类似的东西:

      dim test as DateTime = DateTime.Now
      dim test2 as DateTime = DateTime.Now.AddDays(2)
      dim result as TimeSpan = test.Subtract(test2)
      dim hours as Integer = result.Hours
      dim days as Integer = result.Days
      int years=days mod 365
      days=days-years*365
    

    【讨论】:

      【解决方案2】:

      类似这样的:

          ' A Start date an end Date to test with
          Dim StartingDate As DateTime = DateTime.Now
          Dim TargetEndDate As DateTime = DateTime.Now.AddYears(1).AddDays(5).AddMinutes(45)
      
          ' Get the difference between the two dates, and Create a new Date containing just the total days
          Dim DifferenceBetweenDates As TimeSpan = TargetEndDate.Subtract(StartingDate)
          Dim DiffFromSystemDate As New DateTime(0, 0, DifferenceBetweenDates.TotalDays)
      
          ' Get the number of years, months and days left
          Dim NumberOfYears As Integer = DiffFromSystemDate.Year - 1
          Dim NumberOfMonths As Integer = DiffFromSystemDate.Month - 1
          Dim NumberOfDays As Integer = StartingDate.Day - DateTime.DaysInMonth(StartingDate.Year, StartingDate.Month)
      
          ' Build up the result string
          Dim Result As String = String.Format("{0} YEAR, {1} MONTHS, {3} DAYS LEFT", NumberOfYears, NumberOfMonths, NumberOfDays)
      
      1. 我还没编译
      2. 它不能完全工作(闰年和一年中的日子)

      请参阅 JonSkeets 的重复帖子以获得更好的方法

      【讨论】:

        【解决方案3】:

        我曾经遇到过同样的问题并找到了一个解决方案。 请记住 d1 是 endDate,d2 是 startDate。 d1 > d2

        public static void TimeSpanToDate(DateTime d1, DateTime d2, out int years, out int months, out int days)
        {
            // compute & return the difference of two dates,
            // returning years, months & days
            // d1 should be the larger (newest) of the two dates
            // we want d1 to be the larger (newest) date
            // flip if we need to
            if (d1 < d2)
            {
                DateTime d3 = d2;
                d2 = d1;
                d1 = d3;
            }
        
            // compute difference in total months
            months = 12 * (d1.Year - d2.Year) + (d1.Month - d2.Month);
        
            // based upon the 'days',
            // adjust months & compute actual days difference
            if (d1.Day < d2.Day)
            {
                months--;
                days = DateTime.DaysInMonth(d2.Year, d2.Month) - d2.Day + d1.Day;
            }
            else
            {
                days = d1.Day - d2.Day;
            }
            // compute years & actual months
            years = months / 12;
            months -= years * 12;
        }
        

        对我来说很好用。

        希望对你有帮助。

        普雷文

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-01-23
          • 2021-07-04
          • 1970-01-01
          • 2021-01-24
          • 2021-10-07
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多