简体   繁体   English

计算两个日期之间的年,月,日

[英]Calculate years, months, days between two dates

I am trying to display the years, months and days between two dates in a C# application that I am creating. 我试图在我创建的C#应用​​程序中显示两个日期之间的年,月和日。 (Using console for testing purposes) (使用控制台进行测试)

I'm using NodaTime to achieve this but I am having issues rounding months up. 我正在使用NodaTime来实现这一目标,但是我遇到了数月的问题。

I have read most of the questions and answers here but didn't find anything that I could benefit from. 我已经在这里阅读了大多数问题和答案,但是没有找到任何我可以受益的地方。

Most of the time the function works but sometimes it doesn't add the months if days > 30 (which would add a month) 大多数情况下,该功能有效,但如果天数> 30(有时会增加一个月),则有时它不会添加月份

I am using this code for testing 我正在使用此代码进行测试

LocalDate start = new LocalDate(2017, 10, 16);
LocalDate end = new LocalDate(2018, 1, 15);
Period period = Period.Between(start, end);

Console.WriteLine("{0} years, {1} months, {2} days",
                              period.Years, period.Months, period.Days);
Console.ReadLine();

(2017, 10, 16) and (2018, 1, 15) should display as 3 months but they show as 2 months and 30 days ( I've tried to add +1 to period.Days but it's showing as 31 days after I do that). (2017,10,16)(2018,1,15)应该显示为3 个月,但是它们显示为2个月和30天(我试图将+1添加到period.Days中,但它显示为我之后的31天去做)。 I need the end date included when I display the format. 显示格式时,需要包含结束日期。

Is there anything I can use to achieve what I need ? 有什么我可以用来实现我所需要的? I need to show the correct format: 2 years, 11 months, 3 days 我需要显示正确的格式:2年11个月3天

The issue is indeed that you're using the "include end date in calculation" in timeanddate.com. 问题的确确实是您在timeanddate.com中使用“计算中包括结束日期”。 The solution isn't to add a day to the period in Noda Time - it's to add a day to the end date before you perform the calculation: 解决方案不是在Noda Time的期间中增加一天,而是在执行计算之前的结束日期中增加一天:

Period period = Period.Between(start, end.PlusDays(1));

That will then give you your expected output: 这将为您提供预期的输出:

using System;
using NodaTime;

class Test
{
    static void Main()
    {
        LocalDate start = new LocalDate(2017, 10, 16);
        LocalDate end = new LocalDate(2018, 1, 15);
        Period period = Period.Between(start, end.PlusDays(1));

        Console.WriteLine(
            $"{period.Years} years, {period.Months} months, {period.Days} days");
    }
}

Output: 输出:

0 years, 3 months, 0 days

When thinking about "between" questions like this, I like to try to think of really simple examples. 在考虑像这样的“介于两者之间”的问题时,我喜欢尝试考虑非常简单的示例。

What's the period between January 23rd 2018 and January 24th 2018? 2018年1月23日到2018年1月24日是什么时间? Noda Time and I both say "1 day". 我和Noda Time都说“ 1天”。 (Add 1 day to January 23rd and you get January 24th.) If you're talking about the period of time covered by the date interval of January 23rd 2018 to January 24th 2018, then that's two days. (将1天添加到1月23日,则将获得1月24日。)如果您要谈论的是2018年1月23日至2018年1月24日的日期间隔所覆盖的时间段,则为两天。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM