我想计算两个日期之间的差异? [英] ı want to calculate different between two date?

查看:89
本文介绍了我想计算两个日期之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想计算两个日期之间的差额,但所有月份必须为30天.

所以2月28日和29日都没关系.
整月30天.

我计算出例如:

12.04.2010
12.04.2007 3年0个月0天

当然,您使用datetimepicker输入的日期可能会有所不同.

I want to calculate the difference between two dates, but all months must be 30 days.

So February 28th & 29th doesn''t matter.
All months 30 day.

I calculate lıke that for example:

12.04.2010
12.04.2007 3 year 0 month 0 day

Of course these dates can be different you enter with datetimepicker.

推荐答案

不确定这是否是您想要的(不一定完全是所有月份都必须30天''):

Not sure if this is what you want (don''t quite get the ''all months must be 30 days''):

DateTime now = DateTime.Now;
DateTime yesterday = DateTime.Now.AddDays(-1);

int diff = now.Subtract(yesterday).Days;
Console.WriteLine(diff);


private void CalculateDifference()
{
    int years, months, days;

    years = (dateTimePicker2.Value.Year - dateTimePicker1.Value.Year);
    months = (dateTimePicker2.Value.Month - dateTimePicker1.Value.Month);
    days = (dateTimePicker2.Value.Day - dateTimePicker1.Value.Day);
    
    if (months < 0)
    {
        years--;
        months = months + 12;
    }

    if (days < 0)
    {
        months--;
        days = days + 30;
    }

    textBox1.Text = years + " Years, " + months + " Months, " + days + " Days";
}



对于以下输入,这将给出以下结果

第一次约会 第二次约会 日期差异
10.07.2009 07.07.2010 0年9个月0天
10.07.2009 10.08.2010 1年0个月, 1天
12.16.2009 10.08.2010 0年,9个月,22天
06.08.2009 07.07.2010 0年,0个月,29天
02.07.2010 03.04.2010 0年0个月27天*


*最后一个例子实际上是不准确的,因为二月没有30天.实际上,3月4日和2月7日之间的差额是非-年的25天和a年的26天,而不是27天.但这就是您要的.



This gives the following results for the following inputs

First DateSecond DateDate Difference
10.07.200907.07.20100 Years, 9 Months, 0 Days
10.07.200910.08.20101 Years, 0 Months, 1 Days
12.16.200910.08.20100 Years, 9 Months, 22 Days
06.08.200907.07.20100 Years, 0 Months, 29 Days
02.07.201003.04.20100 Years, 0 Months, 27 Days*


*The last example is actually inaccurate, though, because February does not have 30 days. In actuality, the difference between March 4th and February 7th is 25 days on a non-leap year and 26 on a leap year, not 27. But that''s what you asked for.


基本上,您将/将要创建一年的自定义日历,该日历有12个月,每个月有30天.

由于要求不是正常的日期操作,因此Feb可以具有28或29以及其他几个月的月份31 ...您必须创建自己的DateClass来处理此类操作.

在该类中,定义要使用的常规日期操作.
您的逻辑将基于一年中的12 * 30 = 360天. (每年的30天平均每月分配)

此外,该自定义类也将帮助您设置日期选择器.与目前一样,日期选择器中没有一个仅显示每个月30天.
奇怪的要求,您需要为自己创建一些新东西.
Basically you have/want to create a custom calendar for an year that has 12 months each having 30 days.

Since the requirement is not a normal date manipulations where Feb can have 28 or 29 and few other months 31... you have to create your own DateClass that can handle such operations.

In the class, define normal date manipulations you want to have.
Your logic would be based on 12*30 = 360 days in an year. (30 days equally distributed across the year for each month)

Further, this custom class would help you in your datepicker too. As currently, none of the datepicker just shows 30 days for each month.
Strange requirement, you need to create something new for yourself.


这篇关于我想计算两个日期之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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