如何计算C#中文本框中两个日期之间的差异。 [英] How to calculate difference between two dates in textboxes in C#.

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

问题描述

我有两个文本框,我在其中存储来自日历的日期。我有第三个文本框,我想在c#中计算两个日期之间的差异后存储天数。

我该怎么做? Plz帮助。



谢谢。

I have two textboxes in which I am storing dates from calender. I have a third textbox in which I want to store the number of days after calculating the difference between the two dates in c#.
How can I do that? Plz help.

Thank you.

推荐答案

到目前为止的任何努力? Google [ ^ ]有很多主题。



以下是其中一些:



http://www.dotnetspider.com/resources/458-How-find-difference-between-two- Dates-C-or.aspx [ ^ ]



http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx [ ^ ]



基本上,你需要利用 TimeSpan [ ^ ]。
Any effort so far? Google[^] has hell lot of topics on this.

Here are few of those:

http://www.dotnetspider.com/resources/458-How-find-difference-between-two-Dates-C-or.aspx[^]

http://msdn.microsoft.com/en-us/library/8ysw4sby.aspx[^]

Basically, you need to make use of TimeSpan[^].


这可以使用输入T. imeSpan。例如:假设我们想知道最大值之间的天数。和分钟。 DateTime Type的值并在Console窗口中显示,然后我可能会写出类似的内容:

This can be easily accomplished using an object of Type "TimeSpan". For example: let's assume that we want to know the number of days between the max. and min. values for the DateTime Type and show it in a Console window, then I may write something like:
DateTime d1=DateTime.MinValue;
DateTime d2=DateTime.MaxValue;
TimeSpan span=d2-d1;
Console.WriteLine( "There're {0} days between {1} and {2}" , span.TotalDays, d1.ToString(), d2.ToString() );


假设你的DateTimes被称为start和end。

DateTime start = new DateTime(2009,6,14);

DateTime end = new DateTime(2009,12,14);



我们已将我们的日期时间设定为2009年6月14日和2009年12月14日。



现在,让我们找到两者之间的区别。为此,我们创建一个TimeSpan:

TimeSpan差异=结束 - 开始;



使用此TimeSpan对象,您可以通过多种不同方式表达时间差异。但是,你特意要求天数的差异,所以这里是你如何得到的:

Console.WriteLine(天数差异:+ +差异。天数);



因此,该物业被称为TimeSpan.Days。
Let's say that your DateTimes are called start and end.
DateTime start = new DateTime(2009, 6, 14);
DateTime end = new DateTime(2009, 12, 14);

We have established our DateTimes to June 14, 2009 and December 14, 2009.

Now, let's find the difference between the two. To do this, we create a TimeSpan:
TimeSpan difference = end - start;

With this TimeSpan object, you can express the difference in times in many different ways. However, you specifically asked for the difference in days, so here's how you can get that:
Console.WriteLine("Difference in days: " + difference.Days);

Thus, the property is called TimeSpan.Days.


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

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