Does leap year count 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 count the number of days correctly? (taking into account the leap year)

+2


source to share


2 answers


Yes, it takes that 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);

      

Above outputs: -366 / -365

+8


source


I would suggest loading Boo would be a good addition to your C # development environment . Boo is a Pythonesque scripting language, but with full access to the .NET Framework. I've used it as an aid in a quick attempt at string formatting, regex, and date / time string formatting. Here is my Boo session where I checked your question:

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 into DLLs and EXEs.

0


source







All Articles