Vb.net 中两个日期之间的差异(0 年、0 个月、还剩 0 天)

发布于 2024-12-18 12:37:24 字数 386 浏览 0 评论 0原文

可能的重复:
如何获取两个日期之间的差异年/月/周/日?

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

 1 YEAR, 2 MONTHS, 3 DAYS LEFT

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

Possible Duplicate:
How to get difference between two dates in Year/Month/Week/Day?

I have a problem with the difference between two dates.
I need the out put in 0 YEAR, 0 MONTHS, 0 DAYS LEFTm e.g.:

 1 YEAR, 2 MONTHS, 3 DAYS LEFT

With dateDiff function or using anything else it is not possible.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

·深蓝 2024-12-25 12:37:24

使用 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

Using DateTime as the representation you can have something like:

  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
半城柳色半声笛 2024-12-25 12:37:24

像这样的事情:

    ' 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 帖子以获得更好的方法

Something like this:

    ' 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. I haven't compiled it yet
  2. It doesn't fully work (leap years, and days of the year)

Please see JonSkeets post in the duplicate for a better way of doing this

2024-12-25 12:37:24

我曾经遇到过同样的问题并找到了一个解决方案。
只需记住 d1 是结束日期,d2 是开始日期。 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;
}

对我来说效果很好。

希望有帮助。

普拉文

I got the same issues sometime back and found one solution.
Just remember d1 is endDate and d2 is 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;
}

it works fine for me.

Hope it helps.

Praveen

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文