Click here to Skip to main content
15,915,000 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How to Get Difference between frommonth with fromyear compared to tomonth with toyear....
For instance one has work experience with frommonth March and fromyear 2006 and tomonth June and fromyear 2007 duration should be 1 year and 3 months...Also there would be multiple work experience then all must be summed together...I want to show in months if less than one year..
Posted
Comments
Richard C Bishop 14-Mar-13 10:21am    
If using sql, you can use DateDiff(). That returns an int with the number of days in between the two specified dates.
Korathu 2 14-Mar-13 10:23am    
I am having only frommonth,fromyear,tomonth and toyear in dropdownlists...How to do if so..
[no name] 14-Mar-13 10:33am    
Look the TimeSpan class.

This is a basic logic problem, which is easy to work out if you simply write it out.

From the example you have given, I assume you are working from month start to month end. I.e. Jan 2013 to Jan 2013 would be 1 month; Jan 2012 to Dec 2012 would be 1 year; Jan 2012 to Jan 2013 would be 1 year 1 month.

(Note that for this code it doesn't matter if your months are 1-based or 0-based).

int years = toYear - fromYear;
int months = toMonth - fromMonth + 1; // Add one, because we're assuming beginning of "fromMonth" to end of "toMonth" - i.e. if same month and year given for from and to, then this is 1 month.

// If toMonth is earlier than fromMonth then we'll have a negative number.
// Subtract a year and add 12 to the months.
// E.g. Dec 2012 to Jan 2013 = 1 month.  2013 - 2012 = 1; 1 - 12 (or 0 - 11) = -11 => 0 years, -11 + 12 = 1 month.
if (months < 0)
{
    --years;
    months += 12;
}
// Example tests:
// fromYear = 2006, fromMonth = Mar, toYear = 2007, toMonth June => 1 year 3 months.  (6 - 3 = 3; 2007 - 2006 = 1)
// fromYear = 2012, fromMonth = Mar, toYear = 2012, toMonth = Jun => 3 months. (6 - 3 = 3; 2012 - 2012 = 0)
// fromYear = 2012, fromMonth = Dec, toYear = 2013, toMonth = Jan => 1 month.  (1 - 12 + 1 = -11 => +12 = 1; 2013 -2012 - 1 = 0)
// fromYear = 2010, fromMonth = June, toYear = 2013, toMonth = Feb => 2 years 9 months. (2 - 6 + 1 = -3 => +12 = 3; 2013 - 2012 - 1 = 2)
// fromYear = 2010, fromMonth = Jan, toYear = 2013, toMonth = Jan => 3 years 1 month.  (1 - 1 + 1 = 1; 2013 - 2012 = 3)
// fromYear = 2010, fromMonth = Feb, toYear = 2013, toMonth = Jan => 3 years.  (1 - 2 + 1 = 0; 2013 - 2012 = 3)


After that you need to work out how you want to display this based on "years" and "months". A very simple example (note I've not used any globalization here, etc, and you would probably want to do things like check for the singular "year" and "month", etc.):

if (years > 0)
{
    if (months > 0)
    {
        display = string.Format("{0} years {1} months", years, months);
    }
    else
    {
        display = string.Format("{0} years", years);
    }
}
else
{
    display = string.Format("{1} months", months);
}


Regards,
Ian.
 
Share this answer
 
Read about TimeSpan. You need to find the difference between the to time and the from time. Using Timespan, you get the difference in terms of hours/minutes/seconds, etc.

Refer: MSDN: TimeSpan Structure[^]
 
Share this answer
 
Comments
Ian A Davidson 14-Mar-13 11:44am    
TimeSpan doesn't do years!
Sandeep Mewara 14-Mar-13 11:57am    
Now, do we need any command or calculator for years too?

Will that not be too easy to see what would be the year difference?

BTW, in Timespan, year is considered from days prospective. If you have 1500 days 13 hours then you can easily know how many years using 365 days = 1 year. Right?
Ian A Davidson 14-Mar-13 12:22pm    
With all due respect, you have obviously not read the question.
Sandeep Mewara 14-Mar-13 12:33pm    
It's ok. I did. I was trying to say the same thing that year part is logical thing. Year-month difference can be directly seen via substraction as you have shared in your answer.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900