我怎么能得到两个日期之间的差异 [英] how could i get difference between two dates

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

问题描述

你好朋友,

如果即时通讯在两个文本框中具有日期分别为12/02/2011和14/02/2011,而我想在第三文本框中显示3 .....通过使用 timespan 或其他一些功能
请检查一下......

hello friends,

if i m having date as 12/02/2011 and 14/02/2011 in two text box and i want to display 3 in third text box..... by using timespan or some other function
please check it out......

推荐答案

通过使用TimeSpan;请参阅我为回答此问题而编写的代码示例:问题汇总日期在C#.NET [ ^ ]. (这不仅是求和,我还提到了其他操作:).)

—SA
By using TimeSpan; please see code samples I put in response to this Question: Problem summing dates in C# .NET[^]. (This is not about summing only, I mentioned other operations as well :) .)

—SA


???为什么是3?区别显然是2!
无论如何:
??? Why 3? The difference is obviously 2!
Anyway:
DateTime dt1;
DateTime dt2;
CultureInfo ci = CultureInfo.GetCultureInfo("en-US");
string format = "dd/MM/yyyy";
DateTime.TryParseExact(textBox1.Text, format, ci, DateTimeStyles.None, out dt1);
DateTime.TryParseExact(textBox2.Text, format, ci, DateTimeStyles.None, out dt2);
TimeSpan diff = dt2.Substract(dt1);
textBox3.Text = diff.Days.ToString();


这样的东西...

如果您确实想要3,那么我建议您添加1:


Something like this...

If you really want 3, then I have to suggest you to add 1:

textBox3.Text = (diff.Days + 1).ToString();


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

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