野田佳彦时间:Period.Between返回不正确的值 [英] Noda Time: Period.Between returning incorrect value

查看:176
本文介绍了野田佳彦时间:Period.Between返回不正确的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有NodaTime LIB麻烦。我的目标:计算两个日期之间的年/月/日。所以,这里是我的测试的例子:

 私有静态无效的主要()
    {
        VAR名单=新名单,其中元组LT,日期时间,日期时间>>
        {
            新行<日期时间,日期时间>(新的日期时间(1980年,1,1),新的日期时间(1983,12,31)),
            新行<日期时间,日期时间>(新的日期时间(2009年,1,1),新的日期时间(2015,01,23))
        };
        VAR totalPeriod = Period.Zero;
        的foreach(在列表VAR元组)
        {
            VAR dateFrom = tuple.Item1;
            VAR dateTo = tuple.Item2;
            VAR LD1 =新LocalDate(dateFrom.Year,dateFrom.Month,dateFrom.Day);
            VAR LD2 =新LocalDate(dateTo.Year,dateTo.Month,dateTo.Day);
            VAR周期= Period.Between(LD1,LD2,PeriodUnits.YearMonthDay);
            totalPeriod + =时期;
        }
        Console.WriteLine(岁月:{0},月:{1},天:{2},
            totalPeriod.Years,
            totalPeriod.Months,
            totalPeriod.Days);
        Console.Read();
    }
 

输出为: 年:9月:11天:52

这是错误的我。我想要得到的,例如,下一个输出(当然,输出取决于月份的天数,假设有31天中的月): 年:10,月:0,天数:21

所以,我想这天被四舍五入到年和月。我怎样才能得到呢?

答案: 使用马特的回答,我创建了一个解决方案:

 的foreach(在列表VAR元组)
        {
            VAR dateFrom = tuple.Item1;
            VAR dateTo = tuple.Item2;
            VAR周期= Period.Between(LocalDateTime.FromDateTime(dateFrom).Date,LocalDateTime.FromDateTime(dateTo).Date,PeriodUnits.YearMonthDay);
            totalPeriod + =时期;
        }
        //试图厘清期
        而(totalPeriod.Days> = 30)
        {
            totalPeriod = totalPeriod  -  Period.FromDays(30);
            totalPeriod = totalPeriod + Period.FromMonths(1);
            而(totalPeriod.Months> = 12)
            {
                totalPeriod = totalPeriod  -  Period.FromMonths(12);
                totalPeriod = totalPeriod + Period.FromYears(1);
            }
        }
 

解决方案

理查德就在他对OP评论。问题是,几个月甚至几年都没有明显的量你们自己。一个人必须有一个参照系来算出来。您有参考,当你做了 Period.Between 的操作,但它失去了你的时间尝试的时间加在一起。

如果您检查正在添加的时期,这是有道理的:

 第一:年:3月:11天:30
二:年:6月:0,天数:22
总计:年:9月:11天:52
 

为了圆,你想,22天被添加到30天会在一定程度必须知道哪个月是被引用。即使你保留了原有的信息 - 你会用哪一个?你很可能有一个28天的月份在一边,和一个31天的月份为另一方。

你能做的最好办法是人为地轮结果自己算账,并选择平坦的值(如30天)重新present所有月份。

呵呵,一个小东西(无关你的问题) - 从的DateTime 要转到 LocalDate ,尝试 LocalDateTime.FromDateTime(DT).Date 。 :)

I have a trouble with NodaTime lib. My goal: compute Year/Month/Date between two dates. So, here is my test example:

    private static void Main()
    {
        var list = new List<Tuple<DateTime, DateTime>>
        {
            new Tuple<DateTime, DateTime>(new DateTime(1980, 1, 1), new DateTime(1983, 12, 31)),
            new Tuple<DateTime, DateTime>(new DateTime(2009, 1, 1), new DateTime(2015, 01, 23))
        };
        var totalPeriod = Period.Zero;
        foreach (var tuple in list)
        {
            var dateFrom = tuple.Item1;
            var dateTo = tuple.Item2;
            var ld1 = new LocalDate(dateFrom.Year, dateFrom.Month, dateFrom.Day);
            var ld2 = new LocalDate(dateTo.Year, dateTo.Month, dateTo.Day);
            var period = Period.Between(ld1, ld2, PeriodUnits.YearMonthDay);
            totalPeriod += period;
        }
        Console.WriteLine("Years: {0}, Months: {1}, Days: {2}", 
            totalPeriod.Years, 
            totalPeriod.Months,
            totalPeriod.Days);
        Console.Read();
    }

The output is: Years: 9, Months: 11, Days: 52

It's wrong for me. I want to get, for example, the next output (Of course, the output depends on number of days in month, assuming that there are 31 days in our month): Years: 10, Months: 0, Days: 21

So, I want that days was rounded to years and month. How I can get this?

The answer: Using Matt's answer I created the next solution:

 foreach (var tuple in list)
        {
            var dateFrom = tuple.Item1;
            var dateTo = tuple.Item2;
            var period = Period.Between(LocalDateTime.FromDateTime(dateFrom).Date, LocalDateTime.FromDateTime(dateTo).Date, PeriodUnits.YearMonthDay);
            totalPeriod += period;
        }
        // trying clarify the period
        while(totalPeriod.Days >= 30)
        {
            totalPeriod = totalPeriod - Period.FromDays(30);
            totalPeriod = totalPeriod + Period.FromMonths(1);
            while (totalPeriod.Months >= 12)
            {
                totalPeriod = totalPeriod - Period.FromMonths(12);
                totalPeriod = totalPeriod + Period.FromYears(1);
            }
        }

解决方案

Richard was right in his comment on the OP. The problem is that the months and years aren't distinct quantities unto themselves. One must have a frame of reference to "count" them. You have that reference when you do the Period.Between operation, but it's lost by the time you try to add the periods together.

If you check the periods that are being added, it makes sense:

First:  Years: 3, Months: 11, Days: 30
Second: Years: 6, Months: 0,  Days: 22
Total:  Years: 9, Months: 11, Days: 52

In order to round as you would like, the 22 days being added to the 30 days would somehow have to know which month was being referenced. Even if you retained the original information - which one would you use? You could well have a 28-day month on one side, and a 31-day month on the other.

The best you could do would be to artificially round the results yourself afterwards, and choose a flat value (such as 30 days) to represent all months.

Oh, one minor thing (unrelated to your question) - To go from a DateTime to a LocalDate, try LocalDateTime.FromDateTime(dt).Date. :)

这篇关于野田佳彦时间:Period.Between返回不正确的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆