本文介绍了我想计算两个日期之间的差异?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想计算两个日期之间的差额,但所有月份必须为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.
推荐答案
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 Date | Second Date | Date Difference |
10.07.2009 | 07.07.2010 | 0 Years, 9 Months, 0 Days |
10.07.2009 | 10.08.2010 | 1 Years, 0 Months, 1 Days |
12.16.2009 | 10.08.2010 | 0 Years, 9 Months, 22 Days |
06.08.2009 | 07.07.2010 | 0 Years, 0 Months, 29 Days |
02.07.2010 | 03.04.2010 | 0 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.
这篇关于我想计算两个日期之间的差异?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!