两个日期之间的天数差异 [英] Difference in days between two dates

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

问题描述

我如何获得两个日期之间的区别?
示例:
2011/10/10
2011/10/12
-----------
日:2

注意:29天和31天用于计算

解决方案

您可以使用TimeSpan来检索差异.考虑以下示例:

 DateTime startDate = DateTime.Now.AddDays(-25);
DateTime endDate = DateTime.Now;
TimeSpan差异= endDate.Subtract(startDate);
Console.WriteLine(difference.Days); 


使用DateTime.Subtract(DateTime) 函数.

 DateTime圣诞节=  DateTime( 2011  10 );
DateTime newYears =  DateTime( 2011  10  12 );
TimeSpan span = newYears.Subtract(christmas);

console.Writeline(span); 



来源: http://www.dotnetperls.com/datetime-subtract [ http://www.daniweb.com/web-development/aspnet/threads/324051 [ ^ ]

http://msdn.microsoft.com/en-us/library/aa287590%28v = vs.71%29.aspx [ ^ ]

http://stackoverflow.com/Questions/1083955/how-to-get-to-get-difference-between-两个日期在一年中的月份-周-星期天 [ 解决方案

You use a TimeSpan to retrieve the difference. Consider this example:

DateTime startDate = DateTime.Now.AddDays(-25);
DateTime endDate = DateTime.Now;
TimeSpan difference = endDate.Subtract(startDate);
Console.WriteLine(difference.Days);


Use DateTime.Subtract(DateTime) function.

DateTime christmas = new DateTime(2011, 10, 10);
DateTime newYears = new DateTime(2011, 10, 12);
TimeSpan span = newYears.Subtract(christmas);

console.Writeline(span);



Source:
http://www.dotnetperls.com/datetime-subtract[^]


Refer these:

http://www.daniweb.com/web-development/aspnet/threads/324051[^]

http://msdn.microsoft.com/en-us/library/aa287590%28v=vs.71%29.aspx[^]

http://stackoverflow.com/questions/1083955/how-to-get-difference-between-two-dates-in-year-month-week-day[^]


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

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