如何在两个DateTimePickers之间进行操作以获取天数? [英] how to make an operation between two DateTimePickers to get number of days?

查看:70
本文介绍了如何在两个DateTimePickers之间进行操作以获取天数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在两个DateTimePickers之间进行了一次操作,以获取JUST DAYS的天数(没有几个月没有时间),但是我遇到了一些麻烦.
我执行了这段代码,但是它只在year(2011)期间有效,当日期从流动的year(2012)开始时,

例如:2013年1月1日-2012年12月31日= -365

i did an operation between two DateTimePickers to get number of days JUST DAYS (no months no time), but i face some troubles.
i did this code, it works but just through the period of year(2011), when the date begin in flowing year(2012) ,

for example : 01/01/2013 - 31/12/2012 = -365

TbnbJourLocation.Text =	(DtpDateRetour.Value.DayOfYear -              DtpDateLocation.Value.DayOfYear ).ToString();


我试过了,但这不仅给了我几天的时间,而且还给了我几天,时间,一个月...


and i tried this but it gives me not just number of days, but it''s gives me days , time, month...

TbnbJourLocation.Text = (DtpDateRetour.Value - DtpDateLocation.Value ).ToString();


所以请帮助我,它非常重要.


so please help me it''s very important

推荐答案

您应该使用的是 ^ ].

您将使用以下内容:
What you should use is the Timespan Structure[^].

You would use something like this:
Timespan result = DtpDateRetour.Value - DtpDateLocation.Value;
TbnbJourLocation.Text = Convert.ToInt32( result.TotalDays + 0.5 ).ToString();


此解决方案是对Marcus的回答的补充回答.

这是您可以应用的另一种方法:

This solution is an additional answer to Marcus''s answer.

Here is another way that you can apply:

TimeSpan dateDifference = new TimeSpan();
DateTime beginDate = DtpDateRetour.SelectedDate;
DateTime endDate = DtpDateLocation.SelectedDate;
dateDifference = beginDate.Subtract(endDate);



祝你好运,
OI



Good luck,
OI


您可以使用TimeSpan获取两个日期之间的天差
You can get days difference between two dates using TimeSpan
TimeSpan ts = DtpDateRetour.Value.Subtract(DtpDateLocation.Value);
TbnbJourLocation.Text = ts.Days.ToString();


这篇关于如何在两个DateTimePickers之间进行操作以获取天数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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