简体   繁体   English

在计算两个随机日期之间的天数时,timepan会考虑闰年吗?

[英]does timespan take into account leap year when calculating days between two random dates?

For example: 例如:

DateTime date1 = new DateTime(1955, 12, 12);
DateTime date2 = new DateTime(1967, 3, 6);
TimeSpan fff = date2 - date1;

Will it calculate number of days correctly? 它会正确计算天数吗? (taking leap year into account) (考虑闰年)

Yes, it takes this into account. 是的,它考虑到了这一点。

For proof, try: 为了证明,请尝试:

DateTime date0 = new DateTime(2001, 12, 31);
DateTime date1 = new DateTime(2000, 12, 31);
DateTime date2 = new DateTime(1999, 12, 31);
Console.WriteLine("{0} / {1}", (date2 - date1).Days, (date1-date0).Days);

The above outputs: -366 / -365 以上输出:-366 / -365

I would suggest that a nice complementary tool to your C# development environment would be to download Boo . 我建议你的C#开发环境的一个很好的补充工具是下载Boo Boo is a Pythonesque scripting language, but with full access to the .NET framework. Boo是一种Pythonesque脚本语言,但可以完全访问.NET框架。 I've used it quite a bit as a help in quickly trying out string formatting, regular expressions, and date/time string formatting. 我已经使用它作为快速尝试字符串格式,正则表达式和日期/时间字符串格式的帮助。 Here is my Boo session where I tested out your question: 这是我的Boo会话,我测试了你的问题:

C:\Documents and Settings\Paul>booish
Welcome to booish, an interpreter for the boo programming language.
Running boo 0.9.0.3203 in CLR v2.0.50727.3082.

Enter boo code in the prompt below (or type /help).
>>>dt1 = System.DateTime(2009,1,1)
1/1/2009 12:00:00 AM
>>>dt2 = System.DateTime(2008,1,1)
1/1/2008 12:00:00 AM
>>>dt3 = System.DateTime(2007,1,1)
1/1/2007 12:00:00 AM
>>>print dt1-dt2
366.00:00:00
>>>print dt2-dt3
365.00:00:00
>>>

You can also compile Boo scripts to DLLs and EXEs. 您还可以将Boo脚本编译为DLL和EXE。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM