To get the difference between two dates in C# is easy given the built in functions. The method requires two DateTime variables. In the example below, we will calculate the difference between two string dates.
string startDateString = "1/10/2006"; string endDateString = "3/24/2007"; DateTime startDate = Convert.ToDateTime(startDateString); DateTime endDate = Convert.ToDateTime(endDateString); TimeSpan dateDifference = endDate.Subtract(startDate); int days = dateDifference.Days;
In the example above, two random dates are assigned to strings (startDateString and endDateString). The ToDateTime function of the Convert class is utilized to convert both strings into type DateTime. Finally, the subtract function is called on the future date and the result passed to the TimeSpan class. With the TimeSpan class, the difference in days, hours, milliseconds, minutes, and seconds can be determined. Our example above example is days.
More specifically, the TimeSpan class is capable of returning to either full integers as days, hours, milliseconds, minutes, and seconds. The second option is to return as doubles full and partial days, hours, milliseconds, minutes, and seconds. Additionally, this process will work with the full DateTime format.
RSS Feed
Posted in
Tags: 

how to calculate exact date difference
if i enter 04/08/2008 in first masktextbox1
then second masktextbox2 04/08/1984
then it show 24 years 6 days in textbox1
but actual difference only 24 years
it show 6 days extra due to leap year.
please send me coding with C# in desktop & web application this email id
kgoyal25@yahoo.com
thnks
Hey kapil – granted the script above is giving you the actual day difference, if you want to remove the 6 extra days, you could check each year within the range and subtract one day for each leap year. A simple if statement on each year would look something like:
if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0)
{
return true; // is a leap year
}
return false; // is not a leap year
It was very useful for me.
I searched many sites for help on the Date Difference . This was the most helpful, and easy to launch. Thanks!!
You wrote:
if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0)
I don’t really understand the 400 moment. This condition seems to contradict: how can a number divide by 400 and not divide by 100? :)
I think 400 is redundant. It should look like:
if (year % 4 == 0 && year % 100 != 0)
Yeah Ani,, Thats right
if (year % 4 == 0 && year % 100 != 0)
no need of 400 moment,,
Just bought a Mythic for myself and is going to be receiving one particular for my wife quickly! It has just the correct mix of features. just brief of the total smartphone. I’m retaining an eye about the Captivate for a prospective future mobile phone..
.costs are way as well large when released. I’ve been in large tech for 20+ years and fundamentally hack anything I personal to make it greater laptops, GPS, car navigation systems, and so on. and yes cell phones too. Inside few days I’ve owned the phone I have currently installed Opera Mini, Bolt 2.1, freecaddie, Google Maps w/GPS functionality, deleted all locked useless apps after backing them up and absolutely modified the menu program to my exact needs with no all unproductive apps.